-
Notifications
You must be signed in to change notification settings - Fork 1
/
UserBehavior.php
54 lines (39 loc) · 1.13 KB
/
UserBehavior.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
<?php namespace Inetis\BanBackendUsers;
use Backend\Classes\AuthManager;
use Exception;
use System\Classes\ModelBehavior;
class UserBehavior extends ModelBehavior
{
public function getInetisIsBannedAttribute()
{
if (empty($userId = $this->model->attributes['id'] ?? null)) {
return;
}
$throttle = AuthManager::instance()->findThrottleByUserId($userId, null);
if (!empty($throttle) && $throttle->is_banned) {
return true;
}
return false;
}
/**
* @param $value
*
* @throws Exception
*/
public function setInetisIsBannedAttribute($value)
{
$loggedUser = AuthManager::instance()->getUser();
if (empty($userId = $this->model->attributes['id'] ?? null)) {
return;
}
if ($value && $loggedUser->id == $userId) {
throw new \ApplicationException("You can't ban yourself");
}
$throttle = AuthManager::instance()->findThrottleByUserId($userId, null);
if ($value) {
$throttle->ban();
return;
}
$throttle->unban();
}
}