OrganizationController::behaviors()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 35
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 20
nc 1
nop 0
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\user\controllers;
14
15
use rhosocial\organization\Organization;
16
use Yii;
17
use yii\filters\AccessControl;
18
use yii\filters\VerbFilter;
19
use yii\web\Controller;
20
use yii\web\UnauthorizedHttpException;
21
22
/**
23
 * Organization Controller, designed for user module.
24
 *
25
 * @version 1.0
26
 * @author vistart <[email protected]>
27
 */
28
class OrganizationController extends Controller
29
{
30
    public $layout = '@rhosocial/organization/web/user/views/layouts/organization';
31
    const RESULT_SUCCESS = 'success';
32
    const RESULT_FAILED = 'failed';
33
    const SESSION_KEY_MESSAGE = 'session_key_message';
34
    const SESSION_KEY_RESULT = 'session_key_result';
35
36
    public $viewBasePath = '@rhosocial/organization/web/user/views/organization/';
37
38
    /**
39
     * Get organization by specific parameter.
40
     * @param Organization|string|integer $organization
41
     * @return Organization
42
     */
43
    public function getOrganization($organization)
44
    {
45
        if (!$organization) {
46
            return null;
47
        }
48
        $class = Yii::$app->user->identity->organizationClass;
49
        if ($organization instanceof $class) {
50
            $organization = $organization->getID();
51
        }
52
        if (is_numeric($organization) || is_int($organization)) {
53
            return $class::find()->id($organization)->one();
54
        }
55
        if (is_string($organization) && strlen($organization) == 16) {
56
            return $class::find()->guid($organization)->one();
57
        }
58
        return null;
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function actions()
65
    {
66
        return [
67
            'add-new-member' => [
68
                'class' => 'rhosocial\organization\web\user\controllers\organization\AddNewMemberAction',
69
            ],
70
            'list' => [
71
                'class' => 'rhosocial\organization\web\user\controllers\organization\ListAction',
72
            ],
73
            'revoke' => [
74
                'class' => 'rhosocial\organization\web\user\controllers\organization\RevokeAction',
75
            ],
76
            'view-members' => [
77
                'class' => 'rhosocial\organization\web\user\controllers\organization\ViewMembersAction',
78
            ],
79
            'set-up-organization' => [
80
                'class' => 'rhosocial\organization\web\user\controllers\organization\SetUpOrganizationAction',
81
            ],
82
            'set-up-department' => [
83
                'class' => 'rhosocial\organization\web\user\controllers\organization\SetUpDepartmentAction',
84
            ],
85
        ];
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91
    public function behaviors()
92
    {
93
        return [
94
            'access' => [
95
                'class' => AccessControl::class,
96
                'rules' => [
97
                    [ // Disallow all unauthorized users to access this controller.
98
                        'allow' => false,
99
                        'roles' => ['?'],
100
                    ],
101
                    [ // Disallow user who does not have `setUpOrganization` permission to access this `set-up-organization` action.
102
                        'actions' => ['set-up-organization'],
103
                        'allow' => false,
104
                        'matchCallback' => function ($rule, $action) {
0 ignored issues
show
Unused Code introduced by
The parameter $rule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105
                            return !Yii::$app->user->can('setUpOrganization');
106
                        },
107
                        'denyCallback' => function ($rule, $action) {
0 ignored issues
show
Unused Code introduced by
The parameter $rule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
                            throw new UnauthorizedHttpException(Yii::t('organization', 'You do not have access to set up new organization.'));
109
                        },
110
                    ],
111
                    [
112
                        'allow' => true,
113
                        'roles' => ['@'],
114
                    ]
115
                ],
116
            ],
117
            'verbs' => [
118
                'class' => VerbFilter::class,
119
                'actions' => [
120
                    'deregister' => ['post'],
121
                    'revoke' => ['post'],
122
                ],
123
            ],
124
        ];
125
    }
126
127
    public function actionIndex()
128
    {
129
        return $this->render($this->viewBasePath . 'index');
130
    }
131
132
    public function actionView($id)
133
    {
134
        $user = Yii::$app->user->identity;
135
        $organization = $user->getAtOrganizations()->id($id)->one();
136
        $profile = $organization->profile;
0 ignored issues
show
Unused Code introduced by
$profile is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
137
        return $this->render($this->viewBasePath . 'view');
138
    }
139
140
    public function actionUpdate($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
141
    {
142
        return $this->render($this->viewBasePath . 'update');
143
    }
144
}
145