init() public method

This method overrides the parent implementation by establishing the database connection.
public init ( )
Beispiel #1
0
 /**
  * Initial RBAC action
  * @param integer $id Superadmin ID
  */
 public function actionInit($id = null)
 {
     $auth = new DbManager();
     $auth->init();
     $auth->removeAll();
     //удаляем старые данные
     // Rules
     $groupRule = new GroupRule();
     $auth->add($groupRule);
     // Roles
     $student = $auth->createRole('student');
     $student->description = 'Student';
     $student->ruleName = $groupRule->name;
     $auth->add($student);
     $teacher = $auth->createRole('teacher');
     $teacher->description = 'Teacher';
     $teacher->ruleName = $groupRule->name;
     $auth->add($teacher);
     $auth->addChild($teacher, $student);
     $admin = $auth->createRole('admin');
     $admin->description = 'Admin';
     $admin->ruleName = $groupRule->name;
     $auth->add($admin);
     $auth->addChild($admin, $teacher);
     $superadmin = $auth->createRole('superadmin');
     $superadmin->description = 'Superadmin';
     $superadmin->ruleName = $groupRule->name;
     $auth->add($superadmin);
     $auth->addChild($superadmin, $admin);
     // Superadmin assignments
     if ($id !== null) {
         $auth->assign($superadmin, $id);
     }
 }
Beispiel #2
0
 /**
  * Initial RBAC action
  * @param integer $id Superadmin ID
  */
 public function actionInit($id = null)
 {
     $auth = new DbManager();
     $auth->init();
     $auth->removeAll();
     //удаляем старые данные
     // Rules
     $groupRule = new GroupRule();
     $auth->add($groupRule);
     // Roles
     $user = $auth->createRole('user');
     $user->description = 'User';
     $user->ruleName = $groupRule->name;
     $auth->add($user);
     $moderator = $auth->createRole(' moderator ');
     $moderator->description = 'Moderator ';
     $moderator->ruleName = $groupRule->name;
     $auth->add($moderator);
     $auth->addChild($moderator, $user);
     $admin = $auth->createRole('admin');
     $admin->description = 'Admin';
     $admin->ruleName = $groupRule->name;
     $auth->add($admin);
     $auth->addChild($admin, $moderator);
     $superadmin = $auth->createRole('superadmin');
     $superadmin->description = 'Superadmin';
     $superadmin->ruleName = $groupRule->name;
     $auth->add($superadmin);
     $auth->addChild($superadmin, $admin);
     // Superadmin assignments
     if ($id !== null) {
         $auth->assign($superadmin, $id);
     }
 }
Beispiel #3
0
 public function init()
 {
     parent::init();
     if (\Yii::$app->user->isGuest) {
         return;
     }
 }
Beispiel #4
0
 public function actionInit()
 {
     if (!$this->confirm("Are you sure? It will re-create permissions tree.")) {
         return self::EXIT_CODE_NORMAL;
     }
     //$auth = Yii::$app->authManager;
     // Підключення через Базу даних
     $auth = new DbManager();
     $auth->init();
     $auth->removeAll();
     // Роль студент
     $student = $auth->createRole('student');
     $student->description = 'Student';
     $auth->add($student);
     // Роль працедавець
     $employer = $auth->createRole('employer');
     $employer->description = 'Employer';
     $auth->add($employer);
     // Роль модератор
     $moderator = $auth->createRole('moderator');
     $moderator->description = 'Moderator';
     $auth->add($moderator);
     $auth->addChild($moderator, $student);
     $auth->addChild($moderator, $employer);
     // Роль адміністратор
     $admin = $auth->createRole('admin');
     $admin->description = 'Administrator';
     $auth->add($admin);
     $auth->addChild($admin, $moderator);
 }
Beispiel #5
0
 public function init()
 {
     parent::init();
     if (isset(Yii::$app->user)) {
         $this->assignRole();
     }
 }
 /**
  * This is to be called only once at the initialization
  * by commenting out the behaviors first.
  */
 public function actionAdmin()
 {
     $r = new DbManager();
     $r->init();
     $test = $r->createRole('admin');
     $r->add($test);
     $r->assign($test, Yii::$app->user->id);
 }
Beispiel #7
0
 public function init()
 {
     parent::init();
     //вешаем на событие удаления пользователя удаление всех его назначений в acl
     Event::on(User::class, User::EVENT_USER_DELETE, function (UserEvent $event) {
         return $this->revokeAll($event->user->id);
     });
 }
Beispiel #8
0
 public function afterDelete()
 {
     $rbac = new DbManager();
     $rbac->init();
     $role = $rbac->createRole($this->name);
     $role->description = $this->title;
     $rbac->remove($role);
     $rbac->removeChildren($role);
     return parent::afterDelete();
 }
Beispiel #9
0
 /**
  * when user login in backend , it should be 'Administrator' or ,'Merchant'
  */
 public static function beforeLogin()
 {
     Event::on(\yii\web\User::className(), \yii\web\User::EVENT_BEFORE_LOGIN, function ($event) {
         $user = $event->identity;
         $auth = new DbManager();
         $auth->init();
         $role = $auth->getRolesByUser($user->id);
         $event->isValid = in_array(current($role)->name, ['Administrator', 'Merchant']);
     });
 }
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     $this->db = Instance::ensure($this->db, Connection::className());
     if ($this->enableCaching) {
         $this->cache = Instance::ensure($this->cache, Cache::className());
     } else {
         $this->cache = null;
     }
 }
Beispiel #11
0
 /**
  * load permissions for selected
  * @return array
  */
 public function loadPermissions()
 {
     $auth = new DbManager();
     $auth->init();
     $children = $auth->getChildren($this->role_name);
     $dbPermissions = $this->serializePermissions($children);
     $selectedValue = [];
     foreach ($dbPermissions as $key => $value) {
         $selectedValue[$key] = array_keys($value);
     }
     return $selectedValue;
 }
Beispiel #12
0
 public function init()
 {
     parent::init();
     $host = '127.0.0.1';
     $dbname = 'tmc_test';
     $connection = new Connection(['dsn' => 'mysql:host=' . $host . ';dbname=' . $dbname, 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'tablePrefix' => 't_']);
     $connection->open();
     $this->db = $connection;
     if ($this->cache !== null) {
         $this->cache = Instance::ensure($this->cache, Cache::className());
     }
 }
Beispiel #13
0
 public function actionInit()
 {
     $auth = new DbManager();
     $auth->init();
     $auth->removeAll();
     $groupRule = new GroupRule();
     $auth->add($groupRule);
     $user = $auth->createRole('user');
     $user->description = 'User';
     $user->ruleName = $groupRule->name;
     $auth->add($user);
     $auth->add($auth->createPermission('admin'));
 }
Beispiel #14
0
 public function init()
 {
     if (is_string($this->db)) {
         $this->db = Yii::$app->get($this->db);
     }
     if (!$this->assignmentTable) {
         $this->assignmentTable = $this->db->tablePrefix . 'auth_assignment';
     }
     if (!$this->itemTable) {
         $this->itemTable = $this->db->tablePrefix . 'auth_item';
     }
     if (!$this->itemChildTable) {
         $this->itemChildTable = $this->db->tablePrefix . 'auth_item_child';
     }
     parent::init();
 }
Beispiel #15
0
 public function afterSave($insert, $changedAttributes)
 {
     parent::afterSave($insert, $changedAttributes);
     if (!\Yii::$app instanceof ConsoleApplication) {
         if ($this->scenario == 'update' || $this->scenario == 'create') {
             $auth = new DbManager();
             $auth->init();
             $name = $this->role ? $this->role : self::ROLE_DEFAULT;
             $role = $auth->getRole($name);
             if (!$insert) {
                 $auth->revokeAll($this->id);
             }
             $auth->assign($role, $this->id);
         }
     }
 }
 /**
  * Remove admin role for user
  * after that set member role for user
  * @param $id: user id from user table
  * @return redirect to admin/index page
  */
 public function actionRemoverole($id)
 {
     $r = new DbManager();
     $r->init();
     if ($id > 0) {
         // remove admin role for this user
         $admin = $r->getRole('admin');
         $r->revoke($admin, $id);
         // get member role to add to this user
         $member = $r->getRole('member');
         $r->assign($member, $id);
         // update user table
         $this->updateUser($id, BUser::getAuthName('ROLE_MEMBER'));
         Yii::$app->getSession()->setFlash('user.success', Yii::t('user', 'User has been updated'));
     } else {
         Yii::$app->getSession()->setFlash('user.success', Yii::t('error', 'Sorry there is something wrong!'));
     }
     return $this->redirect(['index']);
 }
Beispiel #17
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     //if (!Yii::$app->user->isGuest) {
     //我们假设用户的角色是存储在身份
     // $this->assign(Yii::$app->user->identity->id, Yii::$app->user->identity->role);
     // }
     //        $user = Yii::$app->getUser();
     //        if (!$user->isGuest) {
     //            $identity = $user->getIdentity();
     //            if (!$this->getAssignment($identity->role, $identity->getId())) {
     //                $role = new Role([
     //                    'name' => $identity->role
     //                ]);
     //                $this->revokeAll($identity->getId());
     //                $this->assign($role, $identity->getId());
     //            }
     //        }
 }
Beispiel #18
0
 public function savePermissions()
 {
     $auth = new DbManager();
     $auth->init();
     $actions = $this->getActions();
     if (strpos($this->controllerClass, '\\') === false) {
         \Yii::$app->session->addFlash('error', \Yii::t('auth', 'wrong data '));
     } else {
         foreach ($actions as $action) {
             if (!$auth->getPermission($this->controllerClass . '_' . $action)) {
                 $permission = $auth->createPermission($this->controllerClass . '_' . $action);
                 if (!$auth->add($permission)) {
                     \Yii::$app->session->addFlash('error', \Yii::t('auth', $action . ' action add failed'));
                 } else {
                     \Yii::$app->session->addFlash('success', \Yii::t('auth', 'add ' . $action . ' action success!'));
                 }
             } else {
                 \Yii::$app->session->addFlash('error', \Yii::t('auth', $action . ' action has already exist'));
             }
         }
     }
 }
 /**
  * Phân quyền quản trị
  */
 public function actionAssigndata()
 {
     $params = \Yii::$app->request->post();
     if (!empty($params)) {
         self::removeAssignmentByUserId($params['id']);
         if (!empty($params['data'])) {
             $dbManager = new DbManager();
             $dbManager->init();
             foreach ($params['data'] as $role) {
                 $assignment = $dbManager->getAssignment($role, $params['id']);
                 if ($assignment == null) {
                     $dbManager->assign($dbManager->getPermission($role), $params['id']);
                 }
             }
         }
         return $this->response(new Response(true, "Cấp quyền cho tài khoản thành công", []));
     }
 }
Beispiel #20
0
 public function init()
 {
     parent::init();
 }
Beispiel #21
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     $this->db = Yii::$app->getModule('accounts')->db;
     parent::init();
 }
Beispiel #22
0
 public function init()
 {
     parent::init();
     //@hass-todo 最好放在管理页面修复..
     $this->fixPermissions();
 }
 /**
  * Init authManager
  */
 public function init()
 {
     parent::init();
     $this->jsonPath = Yii::getAlias('@app') . '/../frontend/build/scripts/states.json';
 }