AddMemberAction::checkAccess()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 7
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
namespace rhosocial\organization\web\organization\controllers\my;
14
15
use rhosocial\organization\exceptions\DisallowMemberJoinOtherException;
16
use rhosocial\organization\exceptions\ExcludeOtherMembersException;
17
use rhosocial\organization\exceptions\NumberOfMembersExceededException;
18
use rhosocial\organization\exceptions\OnlyAcceptCurrentOrgMemberException;
19
use rhosocial\organization\exceptions\OnlyAcceptSuperiorOrgMemberException;
20
use rhosocial\organization\exceptions\UnauthorizedManageMemberException;
21
use rhosocial\organization\Organization;
22
use rhosocial\organization\rbac\permissions\ManageMember;
23
use rhosocial\organization\web\organization\Module;
24
use rhosocial\user\User;
25
use Yii;
26
use yii\base\Action;
27
use yii\web\ConflictHttpException;
28
29
/**
30
 *
31
 * @version 1.0
32
 * @author vistart <[email protected]>
33
 */
34
class AddMemberAction extends Action
35
{
36
    /**
37
     * Check access.
38
     * It will call [[MemberAction::checkAccess()]] first.
39
     * Then, it will check whether the [[$user]] has the permission to manage member of the organization or department.
40
     * If not, the UnauthorizedManageMemberException will be thrown.
41
     *
42
     * @see MemberAction
43
     * @param Organization $org
44
     * @param User $user
45
     * @return boolean
46
     * @throws UnauthorizedManageMemberException
47
     * @throws NumberOfMembersExceededException
48
     */
49
    public static function checkAccess($org, $user)
50
    {
51
        MemberAction::checkAccess($org, $user);
52
        if (!Yii::$app->authManager->checkAccess($user->getGUID(), (new ManageMember)->name, ['organization' => $org])) {
53
            throw new UnauthorizedManageMemberException();
54
        }
55
        if ($org->hasReachedMemberLimit()) {
56
            throw new NumberOfMembersExceededException();
57
        }
58
        return true;
59
    }
60
61
    /**
62
     * Add member.
63
     * @param Organization $org
64
     * @param User|string|integer $user
65
     * @return boolean
66
     * @throws ConflictHttpException
67
     */
68
    protected function addMember($org, &$user)
69
    {
70
        try {
71
            return $org->addMember($user);
72
        } catch (DisallowMemberJoinOtherException $ex) {
73
            throw new ConflictHttpException($ex->getMessage());
74
        } catch (ExcludeOtherMembersException $ex) {
75
            throw new ConflictHttpException($ex->getMessage());
76
        } catch (OnlyAcceptCurrentOrgMemberException $ex) {
77
            throw new ConflictHttpException($ex->getMessage());
78
        } catch (OnlyAcceptSuperiorOrgMemberException $ex) {
79
            throw new ConflictHttpException($ex->getMessage());
80
        }
81
    }
82
83
    public function run($org, $u = null)
84
    {
85
        $organization = Module::getOrganization($org);
86
        $user = Yii::$app->user->identity;
87
        static::checkAccess($organization, $user);
88
89
        // If $u is not empty and the method is Post, it is considered to be a adding member operation.
90
        if (!empty($u) && Yii::$app->request->isPost) {
91
            $member = $u;
92
            if ($this->addMember($organization, $member)) {
93
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
94
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, "($u) " . Yii::t('organization', 'Member added.'));
95
                return $this->controller->redirect(['add-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...
96
            }
97
            Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
98
            Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, Yii::t('organization', 'Failed to add member.'));
99
            return $this->controller->redirect(['add-member','org' => $org]);
100
        }
101
        $searchModel = Yii::$app->user->identity->getSearchModel();
102
        $dataProvider = $searchModel->search(Yii::$app->request->get());
103
        return $this->controller->render('add-member', [
104
            'organization' => $organization,
105
            'dataProvider' => $dataProvider,
106
            'searchModel' => $searchModel
107
        ]);
108
    }
109
}
110