|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Laravel Platfourm package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Longman\Platfourm\Database\Eloquent\EntityLock; |
|
12
|
|
|
|
|
13
|
|
|
use Auth; |
|
14
|
|
|
use Carbon\Carbon; |
|
15
|
|
|
use Longman\Platfourm\Database\Eloquent\EntityLock\EntityLock; |
|
16
|
|
|
|
|
17
|
|
|
trait EntityLockTrait |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
protected $entityLock = true; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Boot the trait for the model. |
|
24
|
|
|
* |
|
25
|
|
|
* @return void |
|
26
|
|
|
*/ |
|
27
|
|
|
/* public static function bootActionLogTrait() |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
static::created(function ($model) { |
|
31
|
|
|
if (!empty($model->actionLogging)) { |
|
32
|
|
|
static::storeActionLog($model, 'create'); |
|
33
|
|
|
} |
|
34
|
|
|
}); |
|
35
|
|
|
|
|
36
|
|
|
static::updated(function ($model) { |
|
37
|
|
|
if (!empty($model->actionLogging)) { |
|
38
|
|
|
static::storeActionLog($model, 'update'); |
|
39
|
|
|
} |
|
40
|
|
|
}); |
|
41
|
|
|
|
|
42
|
|
|
static::deleted(function ($model) { |
|
43
|
|
|
if (!empty($model->actionLogging)) { |
|
44
|
|
|
static::storeActionLog($model, 'delete'); |
|
45
|
|
|
} |
|
46
|
|
|
}); |
|
47
|
|
|
|
|
48
|
|
|
if (method_exists(static::class, 'restored')) { |
|
49
|
|
|
static::restored(function ($model) { |
|
50
|
|
|
if (!empty($model->actionLogging)) { |
|
51
|
|
|
static::storeActionLog($model, 'restore'); |
|
52
|
|
|
} |
|
53
|
|
|
}); |
|
54
|
|
|
} |
|
55
|
|
|
}*/ |
|
56
|
|
|
|
|
57
|
|
|
public function lockIn() |
|
58
|
|
|
{ |
|
59
|
|
|
$entityLock = static::getEntityLockModelInstance(); |
|
60
|
|
|
|
|
61
|
|
|
$model = $this; |
|
62
|
|
|
|
|
63
|
|
|
$user_id = Auth::user() ? Auth::user()->id : ''; |
|
64
|
|
|
|
|
65
|
|
|
$lang = app()->getLocale(); |
|
66
|
|
|
|
|
67
|
|
|
$className = get_class($model); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
$data = [ |
|
70
|
|
|
'user_id' => $user_id, |
|
71
|
|
|
'lang' => $lang, |
|
72
|
|
|
'entity' => static::getModelClassName($model), |
|
73
|
|
|
'scope' => app()->getScope(), |
|
74
|
|
|
'action' => $action, |
|
|
|
|
|
|
75
|
|
|
'before_data' => $before_data, |
|
|
|
|
|
|
76
|
|
|
'after_data' => $after_data, |
|
|
|
|
|
|
77
|
|
|
'request_data' => app('request')->all(), |
|
78
|
|
|
'url' => app('request')->fullUrl(), |
|
79
|
|
|
'referer' => app('request')->server->get('HTTP_REFERER'), |
|
80
|
|
|
'ip_address' => app('request')->ip(), |
|
81
|
|
|
'user_agent' => app('request')->server->get('HTTP_USER_AGENT'), |
|
82
|
|
|
]; |
|
83
|
|
|
|
|
84
|
|
|
$entityLock->create($data); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
View Code Duplication |
protected static function getEntityLockModelInstance() |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
$model = EntityLock::class; |
|
90
|
|
|
if (class_exists(\App\Models\EntityLock::class)) { |
|
91
|
|
|
$model = \App\Models\EntityLock::class; |
|
92
|
|
|
} |
|
93
|
|
|
return new $model; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
public function withoutEntityLock() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->entityLock = false; |
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function isLocked() |
|
104
|
|
|
{ |
|
105
|
|
|
$date = Carbon::now()->subMinutes(5); |
|
106
|
|
|
return $this->entityLock()->where('locked_at', '>=', $date->toDateTimeString()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Polymorphic relationship. Second parameter to morphOne/morphMany |
|
111
|
|
|
* should be the same as the prefix for the *_id/*_type fields. |
|
112
|
|
|
*/ |
|
113
|
|
View Code Duplication |
public function entityLock() |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
|
|
$model = EntityLock::class; |
|
116
|
|
|
if (class_exists(\App\Models\EntityLock::class)) { |
|
117
|
|
|
$model = \App\Models\EntityLock::class; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $this->morphOne($model, 'entity'); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.