Пример #1
0
 /**
  * 获取用户组名称
  *
  * @return array
  */
 public function getGroupNames()
 {
     $names = [];
     $groupIdArr = $this->getGroupIdArr();
     foreach ($groupIdArr as $groupId) {
         $group = DpAdminGroup::getByGroupId($groupId);
         if ($group) {
             $names[] = $group['name'];
         }
     }
     return $names;
 }
Пример #2
0
 /**
  * 保存数据
  *
  * @return array
  */
 public function actionSave()
 {
     $user_id = \Yii::$app->request->post('user_id');
     $menu_ids = \Yii::$app->request->post('menu_ids');
     $username = \Yii::$app->request->post('username');
     $nickname = \Yii::$app->request->post('nickname');
     $source_password = \Yii::$app->request->post('source_password');
     $group_ids = (array) \Yii::$app->request->post('group_ids');
     $is_group_access = \Yii::$app->request->post('is_group_access');
     $is_user_access = \Yii::$app->request->post('is_user_access');
     $is_system = \Yii::$app->request->post('is_system');
     $note = \Yii::$app->request->post('note');
     $status = \Yii::$app->request->post('status');
     $groupIdsStr = '';
     if ($group_ids) {
         $groupIdsStr = join(',', array_filter($group_ids, function ($groupId) {
             return DpAdminGroup::getByGroupId($groupId);
         }));
     }
     $saveData = ['username' => $username, 'nickname' => $nickname, 'source_password' => $source_password, 'group_ids' => $groupIdsStr, 'is_group_access' => $is_group_access, 'is_user_access' => $is_user_access, 'is_system' => $is_system, 'note' => $note, 'status' => $status];
     if ($user_id) {
         $obj = DpAdminUser::find()->where(['user_id' => $user_id])->one();
         if (!$obj) {
             return $this->renderError('保存失败,记录不存在!');
         }
     } else {
         $obj = new DpAdminUser();
     }
     $obj->setAttributes($saveData);
     if (!$obj->save()) {
         foreach ($obj->getErrors() as $error) {
             foreach ($error as $message) {
                 return ['success' => false, 'msg' => $message];
             }
         }
     }
     // 用户关联的菜单权限更新
     DpAdminUserMenuRelation::deleteByUserId($obj->user_id);
     if ($menu_ids) {
         $menuIdArr = array_filter(StringHelper::explode($menu_ids, ',', true, true), function ($menuId) {
             return DpAdminMenu::getByMenuId($menuId);
         });
         if ($menuIdArr) {
             foreach ($menuIdArr as $menuId) {
                 $linkObj = new DpAdminUserMenuRelation();
                 $linkObj->setAttributes(['user_id' => $obj->user_id, 'menu_id' => $menuId]);
                 $linkObj->save();
             }
         }
     }
     return $this->renderSuccess('保存成功');
 }