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() { $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)); }
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)); }