Exemple #1
0
 protected function handle()
 {
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $ids = $request->request->get('ids');
         $session = $this->getSession();
         $db = UserDatabase::getDb();
         $db->transaction();
         try {
             if (!$ids) {
                 throw new \Exception('没有选择任何用户');
             }
             $users = UserAuthModel::allUsers(function (QueryBuilder $qb) use($ids) {
                 $qb->andWhere($qb->expr()->in('id', $ids));
             });
             if (!$users) {
                 throw new \Exception('用户不存在');
             }
             // 删除用户
             foreach ($users as $user) {
                 UserAuthModel::deleteAuth($user['id']);
                 // 删除资料
                 UserProfileModel::deleteProfile($user['id']);
                 // 删除元数据
                 $metadata = UserMetadataModel::allMetadata(function (QueryBuilder $qb) use($user) {
                     $qb->andWhere($qb->expr()->eq('user_id', ':user_id'))->setParameter(':user_id', $user['id']);
                 });
                 foreach ($metadata as $data) {
                     UserMetadataModel::deleteMetadata($user['id'], $data['meta_key']);
                 }
             }
             $db->commit();
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $db->rollback();
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_user_index'));
     } else {
         $ids = $request->query->get('ids');
         if (is_string($ids)) {
             $ids = json_decode($ids, true);
         }
         if (!$ids) {
             throw new \Exception('没有选择任何用户');
         }
         $users = UserAuthModel::allUsers(function (QueryBuilder $qb) use($ids) {
             $qb->andWhere($qb->expr()->in('id', $ids));
         });
         if (!$users) {
             throw new \Exception('用户不存在');
         }
         return $this->render('user/delete.html.twig', array('users' => $users));
     }
 }
Exemple #2
0
 /**
  * @return \Tachigo\User\Database\MySQL\UserDatabase
  */
 public static function getDb()
 {
     return UserDatabase::getDb();
 }
Exemple #3
0
 public function __invoke()
 {
     /** @var \Tachigo\User\Aware\Component\UserAwareHook $hook */
     $hook = $this->getHook();
     if ($hook instanceof UserRegisterFieldsHook) {
         // 如果是用户登录字段配置的钩子
         $hook_results = $hook->getResults();
         $results = array('fields' => array('username' => array('element' => 'input', 'type' => 'text', 'placeholder' => '请输入用户名', 'label' => '用户名'), 'password' => array('element' => 'input', 'type' => 'password', 'placeholder' => '请输入密码', 'label' => '密码'), 'confirm_password' => array('element' => 'input', 'type' => 'password', 'placeholder' => '请输入密码', 'label' => '确认密码'), 'nickname' => array('element' => 'input', 'type' => 'text', 'placeholder' => '请输入昵称', 'label' => '昵称')));
         $hook->setResults(array_merge($hook_results, $results));
     } elseif ($hook instanceof UserRegisterHook) {
         $request = $this->getRequest();
         $session = $this->getSession();
         $posts = $request->request;
         $db = UserDatabase::getDb();
         try {
             $db->transaction();
             $username = $posts->get('username');
             $password = $posts->get('password');
             $confirm_password = $posts->get('confirm_password');
             $nickname = $posts->get('nickname');
             // 检查
             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 != $confirm_password) {
                 throw new \Exception('密码不一致');
             }
             if (!$nickname) {
                 throw new \Exception('昵称不能为空');
             }
             if (strlen($nickname) < 2) {
                 throw new \Exception('昵称至少2个字符');
             }
             // 检查重复
             $user_auth = UserAuthModel::getAuthByUsername($username);
             if ($user_auth) {
                 throw new \Exception('用户名已被占用,请重新选择');
             }
             // 创建用户
             $user_auth = new UserAuthModel();
             $user_auth->username = $username;
             $user_auth->password = md5($password);
             $now = time();
             $user_auth->createTimestamp = $now;
             $user_auth->updateTimestamp = $now;
             $user_auth->loginTimestamp = $now;
             $user_auth->status = 1;
             // 保存
             $user_auth = UserAuthModel::createAuth($user_auth);
             $user_profile = $this->getUserProfile($user_auth->id);
             $user_profile->nickname = $nickname;
             $user_profile = UserProfileModel::saveProfile($user_profile);
             $db->commit();
             $session['auth'] = $user_auth;
             $session['profile'] = $user_profile;
         } catch (\Exception $e) {
             $db->rollback();
             throw $e;
         }
     }
 }
Exemple #4
0
 public function __invoke()
 {
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $posts = $request->request;
         $session = $this->getSession();
         $db = UserDatabase::getDb();
         try {
             $db->transaction();
             $username = $posts->get('username');
             $password = $posts->get('password');
             $repeat_password = $posts->get('repeat_password');
             $nickname = $posts->get('nickname');
             // 检查
             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 (!$nickname) {
                 throw new \Exception('昵称不能为空');
             }
             if (strlen($nickname) < 2) {
                 throw new \Exception('昵称至少2个字符');
             }
             // 检查重复
             $user_auth = UserAuthModel::getAuthByUsername($username);
             if ($user_auth) {
                 throw new \Exception('用户名已被占用,请重新选择');
             }
             // 创建用户
             $user_auth = new UserAuthModel();
             $user_auth->username = $username;
             $user_auth->password = md5($password);
             $now = time();
             $user_auth->createTimestamp = $now;
             $user_auth->updateTimestamp = $now;
             $user_auth->loginTimestamp = $now;
             $user_auth->status = 1;
             // 保存
             $user_auth = UserAuthModel::createAuth($user_auth);
             $user_profile = $this->getUserProfile($user_auth->id);
             $user_profile->nickname = $nickname;
             $user_profile = UserProfileModel::saveProfile($user_profile);
             $db->commit();
             $session['auth'] = $user_auth;
             $session['profile'] = $user_profile;
             $session->addFlash('success', '注册成功');
             // 重定向到完善资料
             return new RedirectResponse($this->generateUrl('user_homepage'));
         } catch (\Exception $e) {
             $db->rollback();
             $session->addFlash('error', $e->getMessage());
             return new RedirectResponse($this->generateUrl('user_register'));
         }
     }
     return $this->render('passport/register.html.twig');
 }