Completed
Push — master ( 88044c...e67d17 )
by vistart
05:48
created

SetUpDepartmentRule::execute()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 8.8142

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 8.2222
ccs 8
cts 12
cp 0.6667
cc 7
eloc 12
nc 12
nop 3
crap 8.8142
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\organization\rbac\rules;
14
15
use rhosocial\user\User;
16
use rhosocial\user\rbac\Item;
17
use rhosocial\organization\Organization;
18
use Yii;
19
use yii\rbac\Rule;
20
21
/**
22
 * @version 1.0
23
 * @author vistart <[email protected]>
24
 */
25
class SetUpDepartmentRule extends Rule
26
{
27
    public $name = "canSetUpDepartment";
28
29
    /**
30
     * 
31
     * @param User $user
32
     * @param Item $item
33
     * @param array $params
34
     */
35 7
    public function execute($user, $item, $params)
36
    {
37 7
        $class = Yii::$app->user->identityClass;
38 7
        if (is_numeric($user) || is_int($user)) {
39
            $user = $class::find()->id($user)->one();
40
        }
41 7
        if (is_string($user)) {
42
            $user = $class::find()->guid($user)->one();
43
        }
44 7
        $organization = $params['organization'];
45
        /* @var $organization Organization */// The Organization or department which is parent.
46
        // If current user is creator of organization or department, he is allowed to set up a child.
47 7
        if (!$user->isOrganizationCreator($organization) && !$user->isOrganizationAdministrator($organization)) {
48
            return false;
49
        }
50 7
        if ($organization->hasReachedSubordinateLimit()) {
51
            return false;
52
        }
53 7
        return true;
54
    }
55
}
56