Passed
Push — master ( f14a0a...1212a9 )
by vistart
04:51
created

SetUpDepartmentRule::execute()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.972

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 8.8571
ccs 7
cts 10
cp 0.7
cc 6
eloc 10
nc 8
nop 3
crap 6.972
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
        return true;
51
    }
52
}
53