protected function handle() { $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $ids = $request->request->get('ids'); $db = AdminDatabase::getDb(); $session = $this->getSession(); $db->transaction(); try { if (!$ids) { throw new \Exception('没有选中任何管理员'); } foreach ($ids as $user_id) { $administrator = UserModel::getUser($user_id); if ($administrator) { UserModel::deleteUser($user_id); } // 删除用户角色关系 $user_roles = UserRoleModel::allRelationship(function (QueryBuilder $qb) use($user_id) { $qb->andWhere($qb->expr()->eq('user_id', ':user_id'))->setParameter(':user_id', $user_id); }); foreach ($user_roles as $user_role) { $role_id = $user_role['role_id']; UserRoleModel::deleteRelationship($user_id, $role_id); // 减少角色的人数 $role = RoleModel::getRole($role_id); if ($role) { $role->userCount -= 1; // 保存 RoleModel::saveRole($role); } } } $session->addFlash('success', '操作成功'); $db->commit(); } catch (\Exception $e) { $db->rollback(); $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('admin_administrator')); } else { $ids = $request->query->get('ids'); $ids = json_decode($ids); if (!$ids) { throw new \Exception('没有选中任何管理员'); } else { $pager = UserModel::listUsers(1, count($ids), function (QueryBuilder $qb) use($ids) { $qb->where($qb->expr()->in('id', $ids)); }); return $this->render('administrator/delete.html.twig', array('users' => $pager)); } } }
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)); }
protected function handle() { $request = $this->getRequest(); $role = RoleModel::getRole($this->id); if (!$role) { throw new \Exception('该角色尚未启用'); } if ($request->getMethod() == 'POST') { $session = $this->getSession(); try { $role->remove(); $session->addFlash('success', '操作成功'); } catch (\Exception $e) { $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('admin_role')); } return $this->render('role/delete.html.twig', array('role' => $role)); }
protected function handle() { $administrator = UserModel::getUser($this->id); $session = $this->getSession(); if (!$administrator) { $session->addFlash('error', '管理员不存在'); return new RedirectResponse($this->generateUrl('admin_administrator')); } // 角色 $founder = RoleModel::getRole('founder'); $roles = $founder->getSubTree(); array_shift($roles); $user_roles = UserRoleModel::allRelationship(function (QueryBuilder $qb) use($administrator) { $qb->where($qb->expr()->eq('user_id', ':user_id'))->setParameter(':user_id', $administrator->id); }); $administrator = $administrator->toArray(); $administrator['roles'] = array(); foreach ($user_roles as $user_role) { $role_id = $user_role['role_id']; $administrator['roles'][$role_id] = $role_id; } $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $session = $this->getSession(); $posts = $request->request; $db = AdminDatabase::getDb(); try { $username = $posts->get('username'); $name = $posts->get('name'); $email = $posts->get('email'); $mobile = $posts->get('mobile'); $qq = $posts->get('qq'); $weixin = $posts->get('weixin'); $roles = $posts->get('roles'); $avatar = $posts->get('avatar'); // 检查 if (!$username) { throw new \Exception('用户名不能为空'); } if (strlen($username) < 2) { throw new \Exception('用户名至少2个字符'); } if (!$name) { throw new \Exception('名称不能为空'); } if (strlen($name) < 2) { throw new \Exception('名称至少2个字符'); } if (!$email) { throw new \Exception('邮箱地址不能为空'); } if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { throw new \Exception('邮箱地址格式不正确'); } if (!$roles) { throw new \Exception('请至少选择一个管理员角色'); } // 查询用户名是否重复 $user = UserModel::getUserByUsername($username); if ($user && $user->id != $this->id) { throw new \Exception("用户名'{$username}'已被占用"); } $db->transaction(); // 清空该用户的角色 foreach ($user_roles as $user_role) { $user_id = $user_role['user_id']; $role_id = $user_role['role_id']; UserRoleModel::deleteRelationship($user_id, $role_id); $role = RoleModel::getRole($role_id); if ($role) { $role->userCount -= 1; RoleModel::saveRole($role); } } $administrator = UserModel::getUser($this->id); $administrator->username = $username; $administrator->name = $name; $administrator->email = $email; $administrator->mobile = $mobile; $administrator->qq = $qq; $administrator->weixin = $weixin; $administrator->avatar = $avatar; $administrator->updateTimestamp = time(); // 保存用户 $administrator = UserModel::saveUser($administrator); // 角色 foreach ($roles as $role_id) { $role = RoleModel::getRole($role_id); if (!$role) { throw new \Exception('管理员角色不存在或者未启用'); } $role->userCount += 1; // 保存 RoleModel::saveRole($role); // 添加角色管理员关系 $user_role = new UserRoleModel(); $user_role->userId = $administrator->id; $user_role->roleId = $role_id; // 创建 UserRoleModel::createRelationship($user_role); } $db->commit(); $session->addFlash('success', '操作成功'); return new RedirectResponse($this->generateUrl('admin_administrator_edit', array('id' => $this->id))); } catch (\Exception $e) { $db->rollback(); $session->addFlash('error', $e->getMessage()); return new RedirectResponse($this->generateUrl('admin_administrator_edit', array('id' => $this->id))); } } return $this->render('administrator/edit.html.twig', array('administrator' => $administrator, 'roles' => $roles)); }
/** * 保存 * @param RoleModel $role * @return RoleModel * @throws \Exception */ public static function saveRole(RoleModel $role) { return self::editRole($role->toArray()); }
protected function handle() { $role = RoleModel::getRole($this->id); if (!$role) { $session = $this->getSession(); $session->addFlash('error', '角色不存在或者未启用'); return new RedirectResponse($this->generateUrl('admin_role')); } $request = $this->getRequest(); $columns = array('ID', '用户名', '邮箱', '手机', 'QQ', '微信', '名称', '创建', '更新'); $fields = array('id', 'username', 'email', 'mobile', 'qq', 'weixin', 'name', 'create_timestamp', 'update_timestamp'); if ($request->isXmlHttpRequest()) { $posts = $request->request; $page_offset = $posts->get('start'); $page_offset = intval($page_offset); $page_size = $posts->get('length'); $page_size = intval($page_size); $is_all = false; if ($page_size < 0) { $is_all = true; } $s_echo = $posts->get('draw'); $s_echo = intval($s_echo); $search = $posts->get('search'); $search_value = $search['value']; $records = array(); $records['data'] = array(); $records['draw'] = $s_echo; $records['recordsTotal'] = 0; $records['recordsFiltered'] = 0; $user_roles = UserRoleModel::allRelationship(function (QueryBuilder $qb) use($role) { $qb->where($qb->expr()->eq('role_id', ':role_id'))->setParameter(':role_id', $role->id); }); $users = array(); foreach ($user_roles as $user_role) { $users[] = $user_role['user_id']; } $users = array_unique($users); if ($is_all) { $page_size = count($users); } $page = $page_offset / $page_size + 1; $pager = UserModel::listUsers($page, $page_size, function (QueryBuilder $qb) use($search_value, $users) { if ($users) { $qb->andWhere($qb->expr()->in('id', $users)); if ($search_value) { $qb->orWhere($qb->expr()->like("`username`", ":username"))->setParameter(":username", "%{$search_value}%"); $qb->orWhere($qb->expr()->like("`name`", ":name"))->setParameter(":name", "%{$search_value}%"); } } else { $qb->andWhere($qb->expr()->eq('id', 0)); } $qb->addOrderBy('create_timestamp', 'desc'); }); $total = $pager->getCount(); $records['recordsTotal'] = $total; $records['recordsFiltered'] = $total; $data = $pager->getData(); foreach ($data as $k => $v) { $line = array(); $line[] = '<input type="checkbox" name="id[]" value="' . $v['id'] . '">'; foreach ($fields as $field) { if (isset($v[$field])) { if ($field == 'create_timestamp' || $field == 'update_timestamp') { // 时间 $line[] = date('Y-m-d H:i:s', $v[$field]); } else { $line[] = $v[$field]; } } } $edit_url = $this->generateUrl('admin_administrator_edit', array('id' => $v['id'])); $line[] = '<a href="' . $edit_url . '"><i class="fa fa-edit"></i> 编辑</a>'; $records['data'][] = $line; } return new JsonResponse($records); } return $this->render('role/users.html.twig', array('role' => $role, 'columns' => $columns)); }
protected function handle() { $session = $this->getSession(); try { $user_info = $this->checkSessionAuth($session, $this->sessionAuthKey); $session->addFlash('success', '欢迎回来, ' . $user_info['username']); return new RedirectResponse($this->generateUrl('admin_homepage')); } catch (\Exception $e) { $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $administrators = $this->getContainer()->getParameter('administrators'); if ($administrators) { $administrators = $administrators->toArray(); } else { $administrators = array(); } $username = $request->request->get('username', ''); $password = $request->request->get('password', ''); // 优先数据库里的配置 $administrator = null; $user = UserModel::getUserByUsername($username); if ($user) { if ($user->password != md5($password)) { if (!isset($administrators[$username])) { $session->addFlash('error', '用户名不存在'); return new RedirectResponse($this->generateUrl('admin_login')); } elseif ($administrators[$username]['password'] != md5($password)) { $session->addFlash('error', '密码错误'); return new RedirectResponse($this->generateUrl('admin_login')); } else { // 找到了 $administrator = $administrators[$username]; } } else { // 找到了 $administrator = $user->toArray(); } } elseif (isset($administrators[$username])) { if ($administrators[$username]['password'] != md5($password)) { $session->addFlash('error', '密码错误'); return new RedirectResponse($this->generateUrl('admin_login')); } else { // 找到了 $administrator = $administrators[$username]; } } if (!$administrator) { // 未找到 $session->addFlash('error', '用户名不存在'); return new RedirectResponse($this->generateUrl('admin_login')); } // 设置角色 if (isset($administrator['id'])) { // 数据库中的管理员 $roles = array(); $user_roles = UserRoleModel::allRelationship(function (QueryBuilder $qb) use($administrator) { $qb->andWhere($qb->expr()->eq('user_id', 'user_id'))->setParameter(':user_id', $administrator['id']); }); foreach ($user_roles as $user_role) { $role_id = $user_role['role_id']; $role_node = RoleModel::getRole($role_id); if ($role_node) { /** @var RoleModel[] $sub_tree */ $sub_tree = $role_node->getSubTree(); foreach ($sub_tree as $role) { $roles[] = $role->id; } } else { $roles[] = $role_id; } } $roles = array_unique($roles); $administrator['roles'] = array_combine(array_values($roles), array_values($roles)); } $this->rememberSession($session, $this->sessionAuthKey, $administrator); $session->addFlash('success', '欢迎回来, ' . $administrator['name']); return new RedirectResponse($this->generateUrl('admin_homepage')); } } return $this->render('passport/login.html.twig'); }
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)); }
protected function handle() { // 查询出所有的角色 $founder = RoleModel::getRole('founder'); $roles = $founder->getSubTree(); array_shift($roles); $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $session = $this->getSession(); $posts = $request->request; $db = AdminDatabase::getDb(); try { $username = $posts->get('username'); $password = $posts->get('password'); $repeat_password = $posts->get('repeat_password'); $name = $posts->get('name'); $email = $posts->get('email'); $mobile = $posts->get('mobile'); $qq = $posts->get('qq'); $weixin = $posts->get('weixin'); $roles = $posts->get('roles'); $avatar = $posts->get('avatar'); // 检查 if (!$username) { throw new \Exception('用户名不能为空'); } if (strlen($username) < 2) { throw new \Exception('用户名至少2个字符'); } if (!$password) { throw new \Exception('密码不能为空'); } if (strlen($password) < 6) { throw new \Exception('密码至少6位数字或者字符'); } if ($password != $repeat_password) { throw new \Exception('确认密码不正确'); } if (!$name) { throw new \Exception('名称不能为空'); } if (strlen($name) < 2) { throw new \Exception('名称至少2个字符'); } if (!$email) { throw new \Exception('邮箱地址不能为空'); } if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { throw new \Exception('邮箱地址格式不正确'); } if (!$roles) { throw new \Exception('请至少选择一个管理员角色'); } // 查询用户名是否重复 $user = UserModel::getUserByUsername($username); if ($user) { throw new \Exception("用户名'{$username}'已被占用"); } $db->transaction(); foreach ($roles as $role_id) { $role = RoleModel::getRole($role_id); if (!$role) { throw new \Exception('管理员角色不存在或者未启用'); } $role->userCount += 1; // 保存 RoleModel::saveRole($role); } // 开始创建 $administrator = new UserModel(); $administrator->username = $username; $administrator->password = md5(trim($password)); $administrator->name = $name; $administrator->email = $email; $administrator->mobile = $mobile; $administrator->qq = $qq; $administrator->weixin = $weixin; $administrator->avatar = $avatar; $now = time(); $administrator->createTimestamp = $now; $administrator->updateTimestamp = $now; // 保存用户 $administrator = UserModel::createUser($administrator); // 添加角色管理员关系 foreach ($roles as $role_id) { $user_role = new UserRoleModel(); $user_role->userId = $administrator->id; $user_role->roleId = $role_id; // 创建 UserRoleModel::createRelationship($user_role); } $db->commit(); $session->addFlash('success', '创建成功'); return new RedirectResponse($this->generateUrl('admin_administrator')); } catch (\Exception $e) { $db->rollback(); $session->addFlash('error', $e->getMessage()); return new RedirectResponse($this->generateUrl('admin_administrator_add')); } } return $this->render('administrator/add.html.twig', array('roles' => $roles)); }