SecurityLogger.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. class SecurityLogger {
  3. public static function action($command, array $logReportData, $timestamp) {
  4. $message = "Unknown SecurityLogger action";
  5. switch($command) {
  6. case "login":
  7. $message = "User " . $logReportData['display_name'] . " (" . $logReportData['user_id'] . ")" .
  8. " logged in to " . DOMAIN;
  9. break;
  10. case "login-failed":
  11. $message = "Failed login on ".DOMAIN .
  12. " using username " . $logReportData['attempted_username'] .
  13. " from IP " . $logReportData['ip_address'];
  14. break;
  15. case "add-user":
  16. $message = "Admin ".$logReportData['admin_display_name']." (".$logReportData['admin_user_id'].
  17. ") added user ".$logReportData['user_added'];
  18. break;
  19. case "edit-user":
  20. $message = "Admin ".$logReportData['display_name']." (".$logReportData['user_id'].
  21. ") edited user ".$logReportData['username'] . "(".$logReportData['edited_user_id'].")";
  22. break;
  23. case "remove-user-domain":
  24. //TODO: send more descriptive names
  25. $message = "Admin ".$logReportData['admin_display_name']." (".$logReportData['admin_user_id'].
  26. ") removed access to account ".$logReportData['domain_removed']." from user ".$logReportData['user_removed'];
  27. break;
  28. case "add-user-domain":
  29. //TODO: send more descriptive names
  30. $message = "Admin ".$logReportData['admin_display_name']." (".$logReportData['admin_user_id'].
  31. ") added access to account ".$logReportData['domain_added']." for user ".$logReportData['user_added'];
  32. break;
  33. case "create-project":
  34. $message = "Admin ".$logReportData['display_name']." (".$logReportData['user_id'].
  35. ") created data project ".$logReportData['project_display_name'] . "(".$logReportData['project_folder'].") in region " . $logReportData['project_bucket'];
  36. break;
  37. case "edit-project":
  38. $message = "Admin ".$logReportData['display_name']." (".$logReportData['user_id'].
  39. ") edited data project ".$logReportData['project_display_name']." (".$logReportData['project_id'].")";
  40. break;
  41. case "add-project-domain":
  42. //TODO: send more descriptive names
  43. $message = "Admin ".$logReportData['admin_display_name']." (".$logReportData['admin_user_id'].
  44. ") added project ".$logReportData['project_added']." access to account ".$logReportData['domain_added'];
  45. break;
  46. case "remove-project-domain":
  47. //TODO: send more descriptive names
  48. $message = "Admin ".$logReportData['admin_display_name']." (".$logReportData['admin_user_id'].
  49. ") removed project ".$logReportData['project_removed']." access to account ".$logReportData['domain_removed'];
  50. break;
  51. case "create-account":
  52. $message = "Admin ".$logReportData['display_name']." (".$logReportData['user_id'].
  53. ") created account ".$logReportData['account_display_name'] . "(".$logReportData['subdomain'].")";
  54. break;
  55. case "edit-account":
  56. $message = "Admin ".$logReportData['display_name']." (".$logReportData['user_id'].
  57. ") edited account ".$logReportData['account_display_name'] . "(".$logReportData['client_id'].")";
  58. break;
  59. }
  60. $db = SqliteDatabase::getSingleton();
  61. $sql = "INSERT INTO security_logs (message, action, recordtime) VALUES (:message, :action, DATETIME('now'))";
  62. $preparedQuery = $db->prepare($sql);
  63. $preparedQuery->bindValue(':message', $message);
  64. $preparedQuery->bindValue(':action', $command);
  65. $preparedQuery->execute();
  66. }
  67. public function read() {
  68. $logs = array();
  69. $db = SqliteDatabase::getSingleton();
  70. $sql = "SELECT * FROM security_logs ORDER BY recordtime DESC LIMIT 0, 20";
  71. $return = $db->query($sql);
  72. while($row = $return->fetchArray(SQLITE3_ASSOC)) {
  73. $logs[] = $row;
  74. }
  75. return $logs;
  76. }
  77. public function readPage($pageOffset = 0, $pageSize = 5) {
  78. $db = SqliteDatabase::getSingleton();
  79. $sql = "SELECT * FROM security_logs ORDER BY recordtime DESC LIMIT ".$pageSize." OFFSET ".($pageOffset * $pageSize).";";
  80. $logs = $db->queryArray($sql);
  81. //var_dump($logs);
  82. return $logs;
  83. }
  84. }