GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

RbacCommand   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 299
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 299
rs 10
c 0
b 0
f 0
wmc 28
lcom 1
cbo 6

10 Methods

Rating   Name   Duplication   Size   Complexity  
A actionBuild() 0 15 1
A actionClear() 0 21 2
A findModules() 0 13 3
A loadModule() 0 31 5
A getNames() 0 23 3
A createRecords() 0 34 5
A getAuthManager() 0 7 2
A initAuthManager() 0 8 1
A createUser() 0 20 2
A assignRoot() 0 31 4
1
<?php
2
/**
3
 * @author Nikita Melnikov <[email protected]>, Sergey Glagolev <[email protected]>
4
 * @link https://github.com/shogodev/argilla/
5
 * @copyright Copyright &copy; 2003-2014 Shogo
6
 * @license http://argilla.ru/LICENSE
7
 * @package frontend.commands
8
 */
9
10
Yii::import('backend.components.*');
11
Yii::import('backend.components.interfaces.*');
12
Yii::import('backend.components.auth.*');
13
Yii::import('backend.components.db.*');
14
Yii::import('backend.controllers.*');
15
16
class RbacCommand extends CConsoleCommand
17
{
18
  public $excludeTasks = array(
19
    'help:help'
20
  );
21
22
  /**
23
   * username администратора
24
   *
25
   * @var string
26
   */
27
  protected $username;
28
29
  /**
30
   * Пароль для администратора
31
   *
32
   * @var string
33
   */
34
  protected $password;
35
36
  /**
37
   * Массив названий модулей
38
   *
39
   * @var array
40
   */
41
  protected $modules = array();
42
43
  /**
44
   * Массив массивов, содержащих, название и имена задач
45
   *
46
   * @var array
47
   */
48
  protected $names = array();
49
50
  /**
51
   * @var CDbAuthManager
52
   */
53
  protected $authManager;
54
55
  /**
56
   * Администратор
57
   *
58
   * @var User
59
   */
60
  protected $user;
61
62
  /**
63
   * @var CAuthItem
64
   */
65
  protected $root;
66
67
  /**
68
   * Системное имя для роли администратора
69
   *
70
   * @var string
71
   */
72
  protected $roleSystemName = 'admin';
73
74
75
  /**
76
   * @param string $username
77
   * @param string $password
78
   */
79
  public function actionBuild($username = 'admin', $password = '123')
80
  {
81
    $this->username = $username;
82
    $this->password = $password;
83
84
    echo "-------------------------------------------------------------".PHP_EOL;
85
    $this->findModules(Yii::getPathOfAlias('backend'));
86
    echo "-------------------------------------------------------------".PHP_EOL;
87
    $this->getNames();
88
89
    $this->createUser();
90
    echo "-------------------------------------------------------------".PHP_EOL;
91
    $this->createRecords();
92
    echo "-------------------------------------------------------------".PHP_EOL;
93
  }
94
95
  /**
96
   * Удаление все информации RBAC
97
   */
98
  public function actionClear()
99
  {
100
    try
101
    {
102
      Yii::import('backend.components.*');
103
      Yii::import('backend.modules.rbac.models.*');
104
105
      BUser::model()->deleteAll();
106
      BRbacTask::model()->deleteAll();
107
108
      $db = new CDbCommand(Yii::app()->db);
109
      $db->delete($this->getAuthManager()->itemChildTable);
110
      $db->delete($this->getAuthManager()->assignmentTable);
111
112
      echo "Выполнено".PHP_EOL;
113
    }
114
    catch( Exception $e )
115
    {
116
      echo $e->getMessage().PHP_EOL;
117
    }
118
  }
119
120
  protected function findModules($basePath)
121
  {
122
    $modulesPath = $basePath.DIRECTORY_SEPARATOR.'modules';
123
124
    foreach(glob($modulesPath.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR) as $moduleDirectory)
125
    {
126
      if( preg_match("/\w+/", basename($moduleDirectory)) )
127
      {
128
        $this->loadModule($moduleDirectory);
129
        $this->findModules($moduleDirectory);
130
      }
131
    }
132
  }
133
134
  protected function loadModule($moduleDirectory)
135
  {
136
    $moduleName = basename($moduleDirectory);
137
138
    Yii::setPathOfAlias($moduleName, $moduleDirectory);
139
    $moduleClass = ucfirst($moduleName).'Module';
140
    Yii::import($moduleName.'.'.$moduleClass);
141
142
    if( class_exists($moduleClass) !== false )
143
    {
144
      if( preg_match_all('/modules\/(\w+)/', $moduleDirectory, $matches) )
145
        $moduleLabel = implode(':', $matches[1]);
146
      else
147
        $moduleLabel = $moduleName;
148
149
      /**
150
       * @var BModule $module
151
       */
152
      $module = new $moduleClass($moduleName, null);
153
154
      if( !$module->enabled )
155
        echo "Модуль $moduleLabel отключен\n";
156
157
      if( empty($module->controllerMap) )
158
        return;
159
160
      echo "Найден модуль: $moduleLabel\n";
161
162
      $this->modules[$moduleName] = $module;
163
    }
164
  }
165
166
  /**
167
   * Создание названий и имен задач
168
   */
169
  protected function getNames()
170
  {
171
    foreach( $this->modules as $moduleName => $module )
172
    {
173
      foreach( $module->controllerMap as $id => $controller )
174
      {
175
        $class = new ReflectionClass($controller);
176
        $properties = $class->getDefaultProperties();
177
178
        $this->names[] = [
179
          'name' => $moduleName.':'.$id,
180
          'title' => $module->name.' - '.$properties['name'],
181
          'enabled' => $properties['enabled']
182
        ];
183
      }
184
    }
185
186
    $this->names[] = [
187
      'name' => 'fileUploader',
188
      'title' => 'Загрузка файлов',
189
      'enabled' => true
190
    ];
191
  }
192
193
  /**
194
   * Создание задач RBAC
195
   */
196
  protected function createRecords()
197
  {
198
    Yii::import('backend.modules.rbac.models.*');
199
200
    $this->assignRoot();
201
202
    foreach( $this->names as $name )
203
    {
204
      if( in_array($name['name'], $this->excludeTasks)  )
205
        continue;
206
207
      $task = new BRbacTask();
208
      $task->name  = $name['name'];
209
      $task->title = $name['title'];
210
      $task->type = CAuthItem::TYPE_TASK;
211
212
      if( !$name['enabled'] )
213
      {
214
        echo "$task->name отключено ".PHP_EOL;
215
        continue;
216
      }
217
218
      try
219
      {
220
        $task->save();
221
        $this->getAuthManager()->addItemChild($this->root->name, $task->name);
222
        echo "$task->title ($task->name) успешно создано".PHP_EOL;
223
      }
224
      catch( Exception $e )
225
      {
226
        echo "$task->name уже создано".PHP_EOL;
227
      }
228
    }
229
  }
230
231
  /**
232
   * @return CDbAuthManager
233
   */
234
  protected function getAuthManager()
235
  {
236
    if( empty($this->authManager) )
237
      $this->initAuthManager();
238
239
    return $this->authManager;
240
  }
241
242
  /**
243
   * Инициализация CDbAuthManager
244
   */
245
  protected function initAuthManager()
246
  {
247
    $this->authManager = new CDbAuthManager();
248
    $this->authManager->itemTable = '{{auth_item}}';
249
    $this->authManager->itemChildTable = '{{auth_item_child}}';
250
    $this->authManager->assignmentTable = '{{auth_assignment}}';
251
    $this->authManager->init();
252
  }
253
254
  /**
255
   * Создание учетной записи администратора
256
   */
257
  protected function createUser()
258
  {
259
    Yii::import('backend.modules.rbac.models.*');
260
    BUser::model()->deleteAllByAttributes(array('username' => $this->username, 'password' => ''));
261
262
    $criteria = new CDbCriteria();
263
    $criteria->compare('username', $this->username);
264
    $this->user = BUser::model()->find($criteria);
265
266
    if( $this->user === null )
267
    {
268
      $this->user = new BUser();
269
      $this->user->username = $this->username;
270
      $this->user->setNewPassword($this->password);
271
      $this->user->save();
272
      echo "Создан пользователь ".$this->username." с паролем ".$this->password.PHP_EOL;
273
    }
274
    else
275
      echo "Пользователь ".$this->username." уже существует".PHP_EOL;
276
  }
277
278
  /**
279
   * Создание прав для администратора
280
   *
281
   * @return void
282
   */
283
  protected function assignRoot()
284
  {
285
    $title = 'Администратор';
286
287
    $criteria = new CDbCriteria();
288
    $criteria->compare('title', $title);
289
    $criteria->compare('type', CAuthItem::TYPE_ROLE);
290
    $criteria->compare('name', $this->roleSystemName);
291
292
    $this->root = BRbacRole::model()->find($criteria);
293
294
    if( empty($this->root) )
295
    {
296
      $this->root = new BRbacRole();
297
      $this->root->name = $this->roleSystemName;
298
      $this->root->title = $title;
299
      $this->root->type  = CAuthItem::TYPE_ROLE;
300
      $this->root->save();
301
    }
302
    else
303
      $this->root = clone $this->root;
304
305
    try
306
    {
307
      if( !$this->getAuthManager()->isAssigned($this->root->name, $this->user->id) )
308
        $this->getAuthManager()->assign($this->root->name, $this->user->id);
309
    }
310
    catch( Exception $e )
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
311
    {
312
    }
313
  }
314
}