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

RevokeDepartmentRule::execute()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7.6024

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 6.9811
ccs 10
cts 13
cp 0.7692
cc 7
eloc 13
nc 12
nop 3
crap 7.6024
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 RevokeDepartmentRule extends Rule
26
{
27
    public $name = "canRevokeDepartment";
28
29
    /**
30
     * 
31
     * @param User $user
32
     * @param Item $item
33
     * @param array $params
34
     */
35 2
    public function execute($user, $item, $params)
36
    {
37 2
        $class = Yii::$app->user->identityClass;
38 2
        if (is_numeric($user) || is_int($user)) {
39
            $user = $class::find()->id($user)->one();
40
        }
41 2
        if (is_string($user)) {
42
            $user = $class::find()->guid($user)->one();
43
        }
44 2
        $organization = $params['organization'];
45
        /* @var $organization Organization */// The department to be revoked.
46
        // If current user is creator of department, he is allowed to revoke it.
47 2
        if ($user->isOrganizationCreator($organization)) {
48 1
            return true;
49
        }
50
        // If current user is creator or administrator of parent one, he is allowed to revoke it.
51 1
        $parent = $organization->parent;
52 1
        if ($user->isOrganizationCreator($parent) || $user->isOrganizationAdministrator($parent)) {
53 1
            return false;
54
        }
55
        return false;
56
    }
57
}
58