Completed
Push — master ( 9eb8f3...27c704 )
by vistart
05:33
created

AssignAdminAction::assignAdmin()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 11
rs 9.4285
c 1
b 0
f 1
cc 3
eloc 8
nc 3
nop 2
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
14
namespace rhosocial\organization\web\organization\controllers\my;
15
16
use rhosocial\organization\exceptions\UnauthorizedCreatorException;
17
use rhosocial\organization\Member;
18
use rhosocial\organization\Organization;
19
use rhosocial\organization\rbac\roles\DepartmentCreator;
20
use rhosocial\organization\rbac\roles\OrganizationCreator;
21
use rhosocial\organization\web\organization\Module;
22
use rhosocial\user\User;
23
use Yii;
24
use yii\base\Action;
25
use yii\base\InvalidCallException;
26
use yii\db\IntegrityException;
27
use yii\web\BadRequestHttpException;
28
use yii\web\ServerErrorHttpException;
29
30
/**
31
 * Class AssignAdminAction
32
 * @package rhosocial\organization\web\organization\controllers\my
33
 * @version 1.0
34
 * @author vistart <[email protected]>
35
 */
36
class AssignAdminAction extends Action
37
{
38
    public $assignAdminSuccessMessage;
39
    public $assignAdminFailedMessage;
40
    public $revokeAdminSuccessMessage;
41
    public $revokeAdminFailedMessage;
42
43
    protected function initMessages()
44
    {
45
        if (!is_string($this->assignAdminSuccessMessage)) {
46
            $this->assignAdminSuccessMessage = Yii::t('organization', 'Administrator assigned.');
47
        }
48
        if (!is_string($this->assignAdminFailedMessage)) {
49
            $this->assignAdminFailedMessage = Yii::t('organization', 'Failed to assign administrator.');
50
        }
51
        if (!is_string($this->revokeAdminSuccessMessage)) {
52
            $this->revokeAdminSuccessMessage = Yii::t('organization' ,'Administrator revoked.');
53
        }
54
        if (!is_string($this->revokeAdminFailedMessage)) {
55
            $this->revokeAdminFailedMessage = Yii::t('organization', 'Failed to revoke.');
56
        }
57
    }
58
59
    public function init()
60
    {
61
        $this->initMessages();
62
        parent::init();
63
    }
64
65
    /**
66
     * @param Organization $org
67
     * @param User $user
68
     * @return boolean
69
     * @throws UnauthorizedCreatorException
70
     */
71
    public static function checkAccess($org, $user)
72
    {
73
        MemberAction::checkAccess($org, $user);
74
        if ($org->isOrganization() && !Yii::$app->authManager->checkAccess($user->getGUID(), (new OrganizationCreator)->name, ['organization' => $org])) {
75
            throw new UnauthorizedCreatorException();
76
        }
77
        if ($org->isDepartment() && !Yii::$app->authManager->checkAccess($user->getGUID(), (new DepartmentCreator)->name, ['organization' => $org])) {
78
            throw new UnauthorizedCreatorException();
79
        }
80
        return true;
81
    }
82
83
    /**
84
     * @param Organization $org
85
     * @param User $user
86
     * @return boolean
87
     * @throws ServerErrorHttpException
88
     * @throws BadRequestHttpException
89
     */
90
    protected function assignAdmin($org, $user)
91
    {
92
        try {
93
            return $org->addAdministrator($user);
94
        } catch (IntegrityException $ex) {
95
            throw new ServerErrorHttpException($ex->getMessage());
96
        } catch (InvalidCallException $ex) {
97
            throw new BadRequestHttpException($ex->getMessage());
98
        }
99
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
100
    }
101
102
    /**
103
     * @param Organization $org
104
     * @param Member|User $user
105
     * @param boolean $keep Keep member after administrator being revoked.
106
     * @return boolean
107
     * @throws ServerErrorHttpException
108
     * @throws BadRequestHttpException
109
     */
110
    protected function revokeAdmin($org, &$user, $keep = true)
111
    {
112
        try {
113
            return $org->removeAdministrator($user, $keep);
114
        } catch (IntegrityException $ex) {
115
            throw new ServerErrorHttpException($ex->getMessage());
116
        } catch (InvalidCallException $ex) {
117
            throw new BadRequestHttpException($ex->getMessage());
118
        }
119
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
120
    }
121
122
    /**
123
     * @param $org
124
     * @param $user
125
     * @param string $revoke
126
     * @return string rendering results.
127
     */
128
    public function run($org, $user, $revoke = '0')
129
    {
130
        $organization = Module::getOrganization($org);
131
        static::checkAccess($organization, Yii::$app->user->identity);
132
133
        if ($revoke == '1') {
134
            if ($this->revokeAdmin($organization, $user)) {
135
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
136
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->revokeAdminSuccessMessage);
137
                return $this->controller->redirect(['member', 'org' => $org]);
0 ignored issues
show
Bug introduced by
The method redirect does only exist in yii\web\Controller, but not in yii\base\Controller.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
138
            }
139
            Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
140
            Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->revokeAdminFailedMessage);
141
            return $this->controller->redirect(['member', 'org' => $org]);
142
        } elseif ($revoke == '0') {
143
            if ($this->assignAdmin($organization, $user)) {
144
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
145
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->assignAdminSuccessMessage);
146
                return $this->controller->redirect(['member', 'org' => $org]);
147
            }
148
            Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
149
            Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->assignAdminFailedMessage);
150
            return $this->controller->redirect(['member', 'org' => $org]);
151
        }
152
        return $this->controller->redirect(['member', 'org' => $org]);
153
    }
154
}
155