This repository has been archived by the owner on Jun 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
ActivitylogSupervisor.php
129 lines (105 loc) · 2.91 KB
/
ActivitylogSupervisor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace Spatie\Activitylog;
use Illuminate\Config\Repository;
use Illuminate\Contracts\Auth\Guard;
use Spatie\Activitylog\Handlers\BeforeHandler;
use Spatie\Activitylog\Handlers\DefaultLaravelHandler;
use Request;
use Config;
class ActivitylogSupervisor
{
/**
* @var array logHandlers
*/
protected $logHandlers = [];
protected $auth;
protected $config;
/**
* Create the logsupervisor using a default Handler
* Also register Laravels Log Handler if needed.
*
* @param Handlers\ActivitylogHandlerInterface $logHandler
* @param Repository $config
* @param Guard $auth
*/
public function __construct(Handlers\ActivitylogHandlerInterface $logHandler, Repository $config, Guard $auth)
{
$this->config = $config;
$this->logHandlers[] = $logHandler;
if ($this->config->get('activitylog.alsoLogInDefaultLog')) {
$this->logHandlers[] = new DefaultLaravelHandler();
}
$this->auth = $auth;
}
/**
* Log some activity to all registered log handlers.
*
* @param $text
* @param string $userId
*
* @return bool
*/
public function log($text, $userId = '')
{
$userId = $this->normalizeUserId($userId);
if (! $this->shouldLogCall($text, $userId)) {
return false;
}
$ipAddress = Request::getClientIp();
foreach ($this->logHandlers as $logHandler) {
$logHandler->log($text, $userId, compact('ipAddress'));
}
return true;
}
/**
* Clean out old entries in the log.
*
* @return bool
*/
public function cleanLog()
{
foreach ($this->logHandlers as $logHandler) {
$logHandler->cleanLog(Config::get('activitylog.deleteRecordsOlderThanMonths'));
}
return true;
}
/**
* Normalize the user id.
*
* @param object|int $userId
*
* @return int
*/
public function normalizeUserId($userId)
{
if (is_numeric($userId)) {
return $userId;
}
if (is_object($userId)) {
return $userId->id;
}
if ($this->auth->check()) {
return $this->auth->user()->id;
}
if (is_numeric($this->config->get('activitylog.defaultUserId'))) {
return $this->config->get('activitylog.defaultUserId');
};
return '';
}
/**
* Determine if this call should be logged.
*
* @param $text
* @param $userId
*
* @return bool
*/
protected function shouldLogCall($text, $userId)
{
$beforeHandler = $this->config->get('activitylog.beforeHandler');
if (is_null($beforeHandler) || $beforeHandler == '') {
return true;
}
return app($beforeHandler)->shouldLog($text, $userId);
}
}