Test Failed
Pull Request — master (#20)
by
unknown
03:47
created

library/Models/Roles.php (3 issues)

1
<?php
2
declare(strict_types=1);
3
4
namespace Gewaer\Models;
5
6
use Gewaer\Exception\ServerErrorHttpException;
7
use Gewaer\Models\Companies;
8
use Baka\Auth\Models\Companies as BakaCompanies;
9
use Phalcon\Di;
10
use Phalcon\Validation;
11
use Phalcon\Validation\Validator\PresenceOf;
12
use Phalcon\Validation\Validator\StringLength;
13
14
15
16
17
class Roles extends AbstractModel
18
{
19
    /**
20
     *
21
     * @var integer
22
     */
23
    public $id;
24
25
    /**
26
     *
27
     * @var string
28
     */
29
    public $name;
30
31
    /**
32
     *
33
     * @var string
34
     */
35
    public $description;
36
37
    /**
38
     *
39
     * @var integer
40
     */
41
    public $scope;
42
43
    /**
44
     *
45
     * @var integer
46
     */
47
    public $companies_id;
48
49
    /**
50
     *
51
     * @var int
52
     */
53
    public $apps_id;
54
55
    /**
56
     *
57
     * @var string
58
     */
59
    public $created_at;
60
61
    /**
62
     *
63
     * @var string
64
     */
65
    public $updated_at;
66
67
    /**
68
     *
69
     * @var integer
70
     */
71
    public $is_deleted;
72
73
    /**
74
     * Default ACL company
75
     *
76
     */
77
    const DEFAULT_ACL_COMPANY_ID = 0;
78
    const DEFAULT_ACL_APP_ID = 0;
79
80
    /**
81
     * Initialize method for model.
82
     */
83 20
    public function initialize()
84
    {
85 20
        $this->setSource('roles');
86
87 20
        $this->hasMany(
88 20
            'id',
89 20
            'Gewaer\Models\AccessList',
90 20
            'roles_id',
91 20
            ['alias' => 'accesList']
92
        );
93 20
    }
94
95
    /**
96
     * Validations and business logic
97
     */
98 3
    public function validation()
99
    {
100 3
        $validator = new Validation();
101
102 3
        $validator->add(
103 3
            'name',
104 3
            new PresenceOf([
105 3
                'field' => 'name',
106
                'required' => true,
107
            ])
108
        );
109
110 3
        $validator->add(
111 3
            'description',
112 3
            new PresenceOf([
113 3
                'field' => 'description',
114
                'required' => true,
115
            ])
116
        );
117
118 3
        $validator->add(
119 3
            'name',
120 3
            new StringLength([
121 3
                'max' => 32,
122 3
                'messageMinimum' => _('Role Name. Maxium 32 characters.'),
123
            ])
124
        );
125
126 3
        return $this->validate($validator);
127
    }
128
129
    /**
130
     * Returns table name mapped in the model.
131
     *
132
     * @return string
133
     */
134 19
    public function getSource(): string
135
    {
136 19
        return 'roles';
137
    }
138
139
    /**
140
     * Get the entity by its name
141
     *
142
     * @param string $name
143
     * @return void
144
     */
145
    public static function getByName(string $name)
146
    {
147
        return self::findFirst([
1 ignored issue
show
Bug Best Practice introduced by
The expression return self::findFirst(a...)->getApp()->getId()))) returns the type Phalcon\Mvc\Model which is incompatible with the documented return type void.
Loading history...
148
            'conditions' => 'name = ?0 AND companies_id = ?1 AND apps_id = ?2 AND is_deleted = 0',
149
            'bind' => [$name, Di::getDefault()->getUserData()->default_company, Di::getDefault()->getApp()->getId()]
150
        ]);
151
    }
152
153
    /**
154
     * Get the entity by its name
155
     *
156
     * @param string $name
157
     * @return void
158
     */
159 1
    public static function getById(int $id)
160
    {
161 1
        return self::findFirst([
1 ignored issue
show
Bug Best Practice introduced by
The expression return self::findFirst(a...EWAER_DEFAULT_APP_ID))) returns the type Phalcon\Mvc\Model which is incompatible with the documented return type void.
Loading history...
162 1
            'conditions' => 'id = ?0 AND companies_id in (?1, ?2) AND apps_id in (?3, ?4) AND is_deleted = 0',
163 1
            'bind' => [$id, Di::getDefault()->getUserData()->default_company, Apps::GEWAER_DEFAULT_APP_ID, Di::getDefault()->getApp()->getId(), Apps::GEWAER_DEFAULT_APP_ID]
164
        ]);
165
    }
166
167
    /**
168
     * Get the Role by it app name
169
     *
170
     * @param string $role
171
     * @return Roles
172
     */
173 7
    public static function getByAppName(string $role, Companies $company): Roles
174
    {
175
        //echeck if we have a dot , taht means we are sending the specific app to use
176 7
        if (strpos($role, '.') === false) {
177
            throw new ServerErrorHttpException('ACL - We are expecting the app for this role');
178
        }
179
180 7
        $appRole = explode('.', $role);
181 7
        $role = $appRole[1];
182 7
        $appName = $appRole[0];
183
184
        //look for the app and set it
185 7
        if (!$app = Apps::getACLApp($appName)) {
186
            throw new ServerErrorHttpException('ACL - No app found for this role');
187
        }
188
189 7
        return self::findFirst([
1 ignored issue
show
Bug Best Practice introduced by
The expression return self::findFirst(a...FAULT_ACL_COMPANY_ID))) returns the type Phalcon\Mvc\Model which includes types incompatible with the type-hinted return Gewaer\Models\Roles.
Loading history...
190 7
            'conditions' => 'apps_id in (?0, ?1) AND companies_id in (?2 , ?3)',
191 7
            'bind' => [$app->getId(), self::DEFAULT_ACL_APP_ID, $company->getId(), self::DEFAULT_ACL_COMPANY_ID]
192
        ]);
193
    }
194
195
    /**
196
     * Duplicate a role with it access list
197
     *
198
     * @return bool
199
     */
200
    public function copy(): Roles
201
    {
202
        $accesList = $this->accesList;
203
204
        //remove id to create new record
205
        $this->name .= 'Copie';
206
        $this->scope = 1;
207
        $this->id = null;
208
        $this->companies_id = $this->di->getUserData()->default_company;
209
        $this->apps_id = $this->di->getApp()->getId();
210
        $this->save();
211
212
        foreach ($accesList as $access) {
213
            $copyAccessList = new AccessList();
214
            $copyAccessList->apps_id = $this->apps_id;
215
            $copyAccessList->roles_id = $this->getId();
216
            $copyAccessList->roles_name = $this->name;
217
            $copyAccessList->resources_name = $access->resources_name;
218
            $copyAccessList->access_name = $access->access_name;
219
            $copyAccessList->allowed = $access->allowed;
220
            $copyAccessList->create();
221
        }
222
223
        return $this;
224
    }
225
}
226