Beispiel #1
0
 protected function handle()
 {
     // 检查
     $role = RoleModel::getRole($this->id);
     if ($role) {
         throw new \Exception('角色已经使用了');
     }
     $administration_roles = $this->getContainer()->getParameter('administration_roles');
     if ($administration_roles) {
         $administration_roles = $administration_roles->toArray();
     } else {
         $administration_roles = array();
     }
     if (!isset($administration_roles[$this->id])) {
         throw new \Exception('角色不存在');
     }
     $administration_role = $administration_roles[$this->id];
     $administration_role['id'] = $this->id;
     $tree = RoleModel::getTree();
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $parent_role_id = $request->request->get('parent_id');
         $session = $this->getSession();
         try {
             $parent_role = RoleModel::getRole($parent_role_id);
             if (!$parent_role) {
                 throw new \Exception('父角色不存在或者没有被使用');
             }
             $role = new RoleModel($administration_role);
             $parent_role->createChildNode($role->toArray());
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_role'));
     }
     return $this->render('role/add.html.twig', array('tree' => $tree, 'role' => $administration_role));
 }
Beispiel #2
0
 /**
  * 保存
  * @param RoleModel $role
  * @return RoleModel
  * @throws \Exception
  */
 public static function saveRole(RoleModel $role)
 {
     return self::editRole($role->toArray());
 }
Beispiel #3
0
 protected function handle()
 {
     $administration_roles = $this->getContainer()->getParameter('administration_roles');
     if ($administration_roles) {
         $administration_roles = $administration_roles->toArray();
     } else {
         $administration_roles = array();
     }
     foreach ($administration_roles as $id => $role) {
         $role['id'] = $id;
         $administration_roles[$id] = $role;
     }
     $founder = RoleModel::getRole('founder');
     if (!$founder) {
         // 创建创始人角色
         $founder = new RoleModel();
         $founder->id = $administration_roles['founder']['id'];
         $founder->name = $administration_roles['founder']['name'];
         $founder->description = $administration_roles['founder']['description'];
         $founder->preOrderTreeLeft = 0;
         $founder->preOrderTreeRight = 1;
         // 保存
         $founder = RoleModel::createRole($founder);
     }
     // 超级管理员
     $super_admin = RoleModel::getRole('super_admin');
     if (!$super_admin) {
         // 创建超级管理员角色
         $super_admin = new RoleModel();
         $super_admin->id = $administration_roles['super_admin']['id'];
         $super_admin->name = $administration_roles['super_admin']['name'];
         $super_admin->description = $administration_roles['super_admin']['description'];
         // 保存
         $founder->createChildNode($super_admin->toArray());
         // 重新获得创始人
         $founder = RoleModel::getRole('founder');
     }
     // 开发人员
     $developer = RoleModel::getRole('developer');
     if (!$developer) {
         // 创建开发者角色
         $developer = new RoleModel();
         $developer->id = $administration_roles['developer']['id'];
         $developer->name = $administration_roles['developer']['name'];
         $developer->description = $administration_roles['developer']['description'];
         // 保存
         $founder->createChildNode($developer->toArray());
         $founder = RoleModel::getRole('founder');
     }
     /** @var RoleModel[] $roles_tree */
     $roles_tree = $founder->getSubTree();
     $list = array();
     foreach ($roles_tree as $i => $role) {
         $list[$role->id] = $role;
     }
     $unsaved_roles = array();
     foreach ($administration_roles as $administration_role) {
         $role_id = $administration_role['id'];
         if (!isset($list[$role_id])) {
             $unsaved_roles[] = $administration_role;
         }
     }
     // 删除创始人这个角色
     if (isset($list['founder'])) {
         unset($list['founder']);
     }
     RoleBusiness::preOrderTree2RecursiveTree($list, $founder);
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $session = $this->getSession();
         try {
             $founder_role = $founder->toArray();
             if (isset($founder_role['depth'])) {
                 unset($founder_role['depth']);
             }
             if (isset($founder_role['width'])) {
                 unset($founder_role['width']);
             }
             if (isset($founder_role['children'])) {
                 unset($founder_role['children']);
             }
             // 保存创始人
             RoleModel::editRole($founder_role);
             $serialized_output = $request->request->get('serialized_output');
             $serialized_output = trim($serialized_output);
             $serialized_output = json_decode($serialized_output, true);
             $tree = RoleBusiness::formatTree($serialized_output, $list);
             $founder->children = $tree;
             $list = RoleBusiness::recursiveTree2PreOrderTree($founder);
             foreach ($list as $role) {
                 $role = $role->toArray();
                 if (isset($role['depth'])) {
                     unset($role['depth']);
                 }
                 if (isset($role['width'])) {
                     unset($role['width']);
                 }
                 if (isset($role['children'])) {
                     unset($role['children']);
                 }
                 // 保存根菜单
                 RoleModel::editRole($role);
             }
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_role'));
     }
     return $this->render('role/index.html.twig', array('founder' => $founder, 'rest' => $unsaved_roles));
 }