public function getCurrentRoleKeys() { $identity = Yii::$app->user->identity; $role_id = $identity->role; $AdminRole = new AdminRole(); # 缓存读取role key if (!CCache::get(CCache::ALL_ROLE_KEY_CACHE_HANDLE)) { CCache::set(CCache::ALL_ROLE_KEY_CACHE_HANDLE, $AdminRole->getAllRoleMenuRoleKey()); } $roleKeys = CCache::get(CCache::ALL_ROLE_KEY_CACHE_HANDLE); //var_dump($roleKeys);exit; return isset($roleKeys[$role_id]) ? $roleKeys[$role_id] : ''; }
public static function ifCurrentUserIsSpecificRole($role_name) { if (!$role_name) { return false; } $user = Yii::$app->user->identity; $user_id = $user['id']; $roles = AdminUserRole::find()->asArray()->where(['user_id' => $user_id])->all(); $role_ids = []; if (!empty($roles)) { foreach ($roles as $role) { $role_ids[] = $role['role_id']; } } $user_role_names = []; $user_roles = AdminRole::find()->asArray()->where(['in', 'role_id', $role_ids])->all(); if (!empty($user_roles)) { foreach ($user_roles as $one) { $user_role_names[] = $one['role_name']; } } if (in_array($role_name, $user_role_names)) { return true; } return false; }
public function getAllRoleIds() { $data = AdminRole::find()->asArray()->select(['role_id'])->all(); $role_ids = []; if (!empty($data)) { foreach ($data as $d) { $role_ids[] = $d['role_id']; } } return $role_ids; }
public function getTableFieldArr() { $table_th_bar = [['orderField' => 'id', 'label' => 'ID', 'width' => '110', 'align' => 'center'], ['orderField' => 'username', 'label' => '用户名称', 'width' => '110', 'align' => 'center'], ['orderField' => 'person', 'label' => '姓名', 'width' => '110', 'align' => 'center'], ['orderField' => 'role', 'width' => '110', 'align' => 'left', 'display' => AdminRole::getAdminRoleArr()], ['orderField' => 'email', 'width' => '110', 'align' => 'center'], ['orderField' => 'created_at_datetime', 'width' => '190', 'align' => 'center'], ['orderField' => 'updated_at_datetime', 'width' => '190', 'align' => 'center'], ['orderField' => 'auth_key', 'width' => '190', 'align' => 'center'], ['orderField' => 'status', 'width' => '60', 'align' => 'center', 'display' => ['1' => '激活', '10' => '关闭']], ['orderField' => 'access_token', 'align' => 'center']]; return $table_th_bar; }
</div> </fieldset> <fieldset id="fieldset_table_qbe"> <legend style="color:#cc0000">ROLE信息</legend> <div> <div class="checkbox-list"> <?php $role_arr = AdminRole::getAdminRoleArr(); foreach ($role_arr as $id => $label) { $checked = ''; if (in_array($id, $role_ids)) { $checked = 'checked="true"'; } ?> <label class="checkbox-inline"> <div id="uniform-inlineCheckbox21" class="checker"> <span><input <?php echo $checked; ?> type="checkbox" value="<?php echo $id; ?> " id="inlineCheckbox" name="role[<?php
public function getCurrentRoleKeys() { $identity = Yii::$app->user->identity; $user_id = $identity->id; $roles = AdminUserRole::find()->asArray()->where(['user_id' => $user_id])->all(); $AdminRole = new AdminRole(); # 缓存读取role key if (!CCache::get(CCache::ALL_ROLE_KEY_CACHE_HANDLE)) { if (!CCache::set(CCache::ALL_ROLE_KEY_CACHE_HANDLE, $AdminRole->getAllRoleMenuRoleKey())) { throw new InvalidValueException('save role key to cache error,check your cache if it can write!'); } } $roleKeys = CCache::get(CCache::ALL_ROLE_KEY_CACHE_HANDLE); //var_dump($roleKeys);exit; //$role_ids = []; $menu_roles = []; if (!empty($roles)) { foreach ($roles as $role) { $role_id = $role['role_id']; $menu_role = isset($roleKeys[$role_id]) ? $roleKeys[$role_id] : []; $menu_roles = array_merge($menu_roles, $menu_role); } } return $menu_roles; }
public function delete() { //$request_param = CRequest::param(); //$this->_param = $request_param; //$this->initParam(); if ($role_id = CRequest::param($this->_paramKey)) { $model = AdminRole::findOne([$this->_paramKey => $role_id]); if ($model->role_id) { $innerTransaction = Yii::$app->db->beginTransaction(); try { $model->delete(); # 删除这个role 对应的所有关联的菜单 AdminRoleMenu::deleteAll(['role_id' => $role_id]); $innerTransaction->commit(); } catch (Exception $e) { $innerTransaction->rollBack(); } echo json_encode(["statusCode" => "200", "message" => 'Delete Success!']); exit; } else { echo json_encode(["statusCode" => "300", "message" => "role_id => {$role_id} , is not exist"]); exit; } } else { if ($ids = CRequest::param($this->_paramKey . 's')) { $id_arr = explode(",", $ids); $innerTransaction = Yii::$app->db->beginTransaction(); try { AdminRole::deleteAll(['in', 'role_id', $id_arr]); # 删除这个role 对应的所有关联的菜单 AdminRoleMenu::deleteAll(['in', 'role_id', $id_arr]); $innerTransaction->commit(); } catch (Exception $e) { $innerTransaction->rollBack(); } echo json_encode(["statusCode" => "200", "message" => "{$ids} Delete Success!"]); exit; } } echo json_encode(["statusCode" => "300", "message" => "role_id or ids Param is not Exist!"]); exit; }
public function getEditArr() { return [['label' => '用户名', 'name' => 'username', 'display' => ['type' => 'inputString'], 'require' => 1], ['label' => '密码', 'name' => 'password', 'display' => ['type' => 'inputPassword'], 'require' => 0], ['label' => '邮箱', 'name' => 'email', 'require' => 1, 'display' => ['type' => 'inputEmail']], ['label' => '姓名', 'name' => 'person', 'require' => 1, 'display' => ['type' => 'inputString']], ['label' => '用户状态', 'name' => 'status', 'display' => ['type' => 'select', 'data' => [AdminUserForm::STATUS_ACTIVE => '激活', AdminUserForm::STATUS_DELETED => '关闭']], 'require' => 1, 'default' => AdminUserForm::STATUS_ACTIVE], ['label' => '权限', 'name' => 'role', 'display' => ['type' => 'select', 'data' => AdminRole::getAdminRoleArr()]], ['label' => '出生日期', 'name' => 'birth_date', 'display' => ['type' => 'inputDate']], ['name' => 'auth_key', 'display' => ['type' => 'inputString']], ['name' => 'access_token', 'display' => ['type' => 'inputString']]]; }