From 810dc2190f1504ce303233f7691e133241fb8446 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Wed, 19 Feb 2020 19:38:40 -0700 Subject: [PATCH] Add ModLogService --- app/Services/ModLogService.php | 98 ++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 app/Services/ModLogService.php diff --git a/app/Services/ModLogService.php b/app/Services/ModLogService.php new file mode 100644 index 00000000..cdbf7212 --- /dev/null +++ b/app/Services/ModLogService.php @@ -0,0 +1,98 @@ +log = new \StdClass; + } + + public static function boot() + { + return new self; + } + + public function user(User $user) + { + $this->log->user = $user; + return $this; + } + + public function objectUid($val = null) + { + $this->log->object_uid = $val; + return $this; + } + + public function objectId($val = null) + { + $this->log->object_id = $val; + return $this; + } + + public function objectType($val = null) + { + $this->log->object_type = $val; + return $this; + } + + public function action($val = null) + { + $this->log->action = $val; + return $this; + } + + public function message($val = null) + { + $this->log->message = $val; + return $this; + } + + public function metadata(array $val = null) + { + $this->log->metadata = json_encode($val); + return $this; + } + + public function accessLevel($val = null) + { + if(!in_array($val, ['admin', 'mod'])) { + return $this; + } + $this->log->access_level = $val; + return $this; + } + + public function save($res = false) + { + $log = $this->log; + if(!isset($log->user)) { + throw new \Exception('Invalid ModLog attribute.'); + } + + $ml = new ModLog(); + $ml->user_id = $log->user->id; + $ml->user_username = $log->user->username; + $ml->object_uid = $log->object_uid ?? null; + $ml->object_id = $log->object_id ?? null; + $ml->object_type = $log->object_type ?? null; + $ml->action = $log->action ?? null; + $ml->message = $log->message ?? null; + $ml->metadata = $log->metadata ?? null; + $ml->access_level = $log->access_level ?? 'admin'; + $ml->save(); + + if($res == true) { + return $ml; + } else { + return; + } + } +} \ No newline at end of file