Passed
Push — master ( b5549b...886b4c )
by vistart
04:08
created

SetUpDepartmentRule   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 70%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 28
rs 10
ccs 7
cts 10
cp 0.7

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 0 17 6
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 7
            return true;
49
        }
50
        return false;
51
    }
52
}
53