Test Failed
Push — master ( 3b9ce6...02c765 )
by Maximo
02:14
created

library/Traits/PermissionsTrait.php (1 issue)

1
<?php
0 ignored issues
show
End of line character is invalid; expected "\n" but found "\r\n"
Loading history...
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Traits;
6
7
use Gewaer\Models\Roles;
8
use Gewaer\Models\UserRoles;
9
use Gewaer\Exception\ServerErrorHttpException;
10
use Gewaer\Exception\ModelException;
11
12
/**
13
 * Trait FractalTrait
14
 *
15
 * @package Gewaer\Traits
16
 */
17
trait PermissionsTrait
18
{
19
    /**
20
     * Assigne a user this role
21
     * Example: App.Role
22
     *
23
     * @param string $role
24
     * @return boolean
25
     */
26
    public function assignRole(string $role): bool
27
    {
28
        $role = Roles::getByAppName($role, $this->defaultCompany);
29
30
        if (!$role) {
31
            throw new ServerErrorHttpException('Role not found in DB');
32
        }
33
34
        $userRole = UserRoles::findFirst([
35
            'conditions' => 'users_id = ?0 and roles_id = ?1 and apps_id = ?2 and company_id = ?3',
36
            'bind' => [$this->getId(), $role->getId(), $role->apps_id, $this->default_company]
37
        ]);
38
39
        if (!$userRole) {
40
            $userRole = new UserRoles();
41
            $userRole->users_id = $this->getid();
42
            $userRole->roles_id = $role->getId();
43
            $userRole->apps_id = $role->apps_id;
44
            $userRole->company_id = $this->default_company;
45
            if (!$userRole->save()) {
46
                throw new ModelException((string) current($userRole->getMessages()));
47
            }
48
        }
49
50
        return true;
51
    }
52
53
    /**
54
     * Remove a role for the current user
55
     * Example: App.Role
56
     *
57
     * @param string $role
58
     * @return boolean
59
     */
60
    public function removeRole(string $role): bool
61
    {
62
        $role = Roles::getByAppName($role, $this->defaultCompany);
63
64
        if (!$role) {
65
            throw new ServerErrorHttpException('Role not found in DB');
66
        }
67
68
        $userRole = UserRoles::findFirst([
69
            'conditions' => 'users_id = ?0 and roles_id = ?1 and apps_id = ?2 and company_id = ?3',
70
            'bind' => [$this->getId(), $role->getId(), $role->apps_id, $this->default_company]
71
        ]);
72
73
        if ($userRole) {
74
            return $userRole->delete();
75
        }
76
77
        return false;
78
    }
79
80
    /**
81
     * Check if the user has this role
82
     *
83
     * @param string $role
84
     * @return boolean
85
     */
86
    public function hasRole(string $role): bool
87
    {
88
        $role = Roles::getByAppName($role, $this->defaultCompany);
89
90
        if (!$role) {
91
            throw new ServerErrorHttpException('Role not found in DB');
92
        }
93
94
        $userRole = UserRoles::findFirst([
95
            'conditions' => 'users_id = ?0 and roles_id = ?1 and apps_id = ?2 and company_id = ?3',
96
            'bind' => [$this->getId(), $role->getId(), $role->apps_id, $this->default_company]
97
        ]);
98
99
        if ($userRole) {
100
            return true;
101
        }
102
103
        return false;
104
    }
105
106
    /**
107
     * At this current system / app can you do this?
108
     *
109
     * Example: resource.action
110
     *  Leads.add || leads.updates || lead.delete
111
     *
112
     * @param string $action
113
     * @return boolean
114
     */
115
    public function can(string $action): bool
116
    {
117
        //get current role for this company App.Role
118
        // Section.Action
119
        //action is going to be resource.action so we need to explode it
120
121
        $userRole = UserRoles::findFirst([
122
            'conditions' => 'users_id = ?0 and apps_id in ( ?1, ?2) and company_id = ?3',
123
            'bind' => [$this->getId(), $this->di->getConfig()->app->id, Roles::DEFAULT_ACL_APP_ID, $this->default_company]
124
        ]);
125
126
        if (!$userRole) {
127
            throw new ServerErrorHttpException('ACL - You dont have acces to this role for this app ');
128
        }
129
130
        //if we find the . then les
131
        if (strpos($action, '.') == false) {
132
            throw new ServerErrorHttpException('ACL - We are expecting the resource for this action');
133
        }
134
135
        $action = explode('.', $action);
136
        $resource = $action[0];
137
        $action = $action[1];
138
        $app = $userRole->app->name;
139
140
        return $this->di->getAcl()->isAllowed($userRole->roles->name, ucfirst($app) . '.' . $resource, $action);
141
    }
142
}
143