Exemple #1
0
 public function user_by_id($value, $args, $context, ResolveInfo $info)
 {
     /*$user = array(
           'auth' => array(),
           'profile' => array(),
       );
       $user_auth = UserAuthModel::getAuthByUsername($args['username_like']);
       if ($user_auth) {
           $user['auth'] = $user_auth->toArray();
           $user_profile = UserProfileModel::getProfile($user_auth->id);
           $user['profile'] = $user_profile->toArray();
       }*/
     $user = array();
     $user_auth = UserAuthModel::getAuth($args['id']);
     if ($user_auth) {
         $user = $user_auth->toArray();
         $user_profile = UserProfileModel::getProfile($user_auth->id);
         if ($user_profile) {
             $user += $user_profile->toArray();
         }
         if (!isset($user['avatar']) || empty($user['avatar'])) {
             // todo
             $user['avatar'] = 'http://cdn.tachigo.com/user/img/avatar.gif';
         }
     }
     return $user;
 }
Exemple #2
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 #3
0
 public function __invoke()
 {
     /** @var \Tachigo\User\Aware\Hook\UserHook $hook */
     $hook = $this->getHook();
     $user_id = $hook->getUserId();
     $hook_results = $hook->getResults();
     $hook_results['auth'] = UserAuthModel::getAuth($user_id);
     $hook_results['profile'] = UserProfileModel::getProfile($user_id);
     $hook->setResults($hook_results);
     /*$hook = $this->getHook();
       $user_id = $hook->getUserId();
       $hook_results = $hook->getResults();
       $db = UserDatabase::getDb();
       $hook_results['auth'] = $db->getUserInfo($user_id);
       $hook_results['profile'] = $hook_results['auth'];
       $hook->setResults($hook_results);*/
 }
Exemple #4
0
 public function __invoke()
 {
     /** @var \Tachigo\User\Aware\Component\UserAwareHook $hook */
     $hook = $this->getHook();
     if ($hook instanceof UserLoginFieldsHook) {
         // 如果是用户登录字段配置的钩子
         $hook_results = $hook->getResults();
         $results = array('fields' => array('username' => array('element' => 'input', 'type' => 'text', 'placeholder' => '请输入用户名', 'label' => '用户名'), 'password' => array('element' => 'input', 'type' => 'password', 'placeholder' => '请输入密码', 'label' => '密码'), 'remember' => array('element' => 'input', 'type' => 'checkbox', 'label' => '下次自动登录')));
         $hook->setResults(array_merge($hook_results, $results));
     } elseif ($hook instanceof UserLoginHook) {
         $request = $this->getRequest();
         $session = $this->getSession();
         $posts = $request->request;
         try {
             $username = $posts->get('username');
             $password = $posts->get('password');
             $remember = $posts->get('remember');
             if (!$username) {
                 throw new \Exception('请输入用户名');
             }
             if (!$password) {
                 throw new \Exception('请输入密码');
             }
             $user_auth = UserAuthModel::getAuthByUsername($username);
             if (!$user_auth) {
                 throw new \Exception('用户不存在');
             }
             if ($user_auth->password != md5($password)) {
                 throw new \Exception('密码错误');
             }
             $user_auth->loginTimestamp = time();
             // 保存
             $user_auth = UserAuthModel::saveAuth($user_auth);
             $session['auth'] = $user_auth;
             $session['profile'] = $this->getUserProfile($user_auth->id);
             // 下次自动登录
             if ($remember) {
             }
         } catch (\Exception $e) {
             throw $e;
         }
     }
 }
Exemple #5
0
    /**
     * @param $user_ids
     * @return array
     */
    public function getUsersInfo(array $user_ids)
    {
        if (empty($user_ids)) {
            return array();
        }
        $sql = <<<SQL
SELECT * FROM {@auth_table} AS a LEFT JOIN {@profile_table} AS p ON a.`id` = p.`id` WHERE a.`id` in :id
SQL;
        $auth_table = UserAuthModel::getTableName();
        $profile_table = UserProfileModel::getTableName();
        $sql = strtr($sql, array('{@auth_table}' => $auth_table, '{@profile_table}' => $profile_table));
        $query = $this->getSelectQuery($sql, array(':id' => '(' . implode(',', $user_ids) . ')'));
        $data = $query->getResult();
        $return = array();
        foreach ($data as $row) {
            $return[$row['id']] = $row;
        }
        return $return;
    }
Exemple #6
0
 public function __invoke()
 {
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $session = $this->getSession();
         $posts = $request->request;
         try {
             $username = $posts->get('username');
             $password = $posts->get('password');
             $remember = $posts->get('remember');
             if (!$username) {
                 throw new \Exception('请输入用户名');
             }
             if (!$password) {
                 throw new \Exception('请输入密码');
             }
             $user_auth = UserAuthModel::getAuthByUsername($username);
             if (!$user_auth) {
                 throw new \Exception('用户不存在');
             }
             if ($user_auth->password != md5($password)) {
                 throw new \Exception('密码错误');
             }
             $user_auth->loginTimestamp = time();
             // 保存
             $user_auth = UserAuthModel::saveAuth($user_auth);
             $session['auth'] = $user_auth;
             $session['profile'] = $this->getUserProfile($user_auth->id);
             // 下次自动登录
             if ($remember) {
             }
             // 登录成功
             $session->addFlash('success', '欢迎回来');
             return new RedirectResponse($this->generateUrl('user_homepage'));
         } catch (\Exception $e) {
             $session->addFlash('error', $e->getMessage());
             return new RedirectResponse($this->generateUrl('user_login'));
         }
     }
     return $this->render('passport/login.html.twig');
 }
Exemple #7
0
 private function handlePassword($auth_id)
 {
     $user_auth = UserAuthModel::getAuth($auth_id);
     if (!$user_auth) {
         throw new \Exception('用户不存在');
     }
     $request = $this->getRequest();
     $posts = $request->request;
     $password = $posts->get('password');
     $confirm_password = $posts->get('confirm_password');
     // 密码
     if (!$password) {
         throw new \Exception('密码不能为空');
     }
     if (strlen($password) < 6) {
         throw new \Exception('密码至少6位字符');
     }
     if (!$confirm_password) {
         throw new \Exception('确认密码不能为空');
     }
     if ($password != $confirm_password) {
         throw new \Exception('密码与确认密码不一致');
     }
     $user_auth->password = md5($password);
     // 保存
     UserAuthModel::saveAuth($user_auth);
 }
Exemple #8
0
    protected function handle()
    {
        $request = $this->getRequest();
        $columns = array('ID', '用户名', '邮箱', '手机', '状态', '创建', '登录');
        $fields = array('id', 'username', 'email', 'mobile', 'status', 'create_timestamp', 'login_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;
            if ($is_all) {
                $page_size = UserAuthModel::getCount(function (QueryBuilder $qb) use($search_value) {
                    if ($search_value) {
                        $qb->orWhere($qb->expr()->like("`username`", ":username"))->setParameter(":username", "%{$search_value}%");
                        $qb->orWhere($qb->expr()->like("`email`", ":email"))->setParameter(":email", "%{$search_value}%");
                        $qb->orWhere($qb->expr()->like("`mobile`", ":mobile"))->setParameter(":mobile", "%{$search_value}%");
                    }
                });
            }
            $page = $page_offset / $page_size + 1;
            $pager = UserAuthModel::listAuth($page, $page_size, function (QueryBuilder $qb) use($search_value) {
                if ($search_value) {
                    $qb->orWhere($qb->expr()->like("`username`", ":username"))->setParameter(":username", "%{$search_value}%");
                    $qb->orWhere($qb->expr()->like("`email`", ":email"))->setParameter(":email", "%{$search_value}%");
                    $qb->orWhere($qb->expr()->like("`mobile`", ":mobile"))->setParameter(":mobile", "%{$search_value}%");
                }
                $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') {
                            // 时间
                            $line[] = date('Y-m-d H:i:s', $v[$field]);
                        } elseif ($field == 'login_timestamp') {
                            if ($v[$field]) {
                                $line[] = date('Y-m-d H:i:s', $v[$field]);
                            } else {
                                $line[] = '未登录';
                            }
                        } elseif ($field == 'status') {
                            $status = '<span class="text-success">正常</span>';
                            $line[] = $status;
                        } else {
                            $line[] = $v[$field];
                        }
                    }
                }
                $edit_url = $this->generateUrl('admin_user_edit', array('id' => $v['id']));
                $delete_url = $this->generateUrl('admin_user_delete', array('ids' => array($v['id'])));
                $operation = '<div class="btn-group">
<a href="javascript:void(0);" data-toggle="dropdown" aria-expanded="false">操作 <i class="fa fa-angle-down"></i></a>
<ul class="dropdown-menu pull-right" role="menu">
<li role="presentation"><a href="' . $edit_url . '"><i class="fa fa-edit"></i> 编辑</a></li>
<li role="presentation"><a data-toggle="modal" data-target="#modal" href="' . $delete_url . '"><i class="fa fa-remove"></i> 删除</a></li>
</ul>
</div>';
                $line[] = $operation;
                $records['data'][] = $line;
            }
            return new JsonResponse($records);
        }
        return $this->render('user/index.html.twig', array('columns' => $columns));
    }
Exemple #9
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 #10
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');
 }
Exemple #11
0
 /**
  * @param $id
  * @return null|UserAuthModel
  */
 public function getUserAuthInfo($id)
 {
     $user_auth = UserAuthModel::getAuth($id);
     return $user_auth;
 }
Exemple #12
0
 protected function handle()
 {
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $posts = $request->request;
         $session = $this->getSession();
         try {
             $username = $posts->get('username');
             $email = $posts->get('email');
             $mobile = $posts->get('mobile');
             $password = $posts->get('password');
             $confirm_password = $posts->get('confirm_password');
             if (!$username) {
                 throw new \Exception('用户名不能为空');
             }
             if (strlen($username) < 2) {
                 throw new \Exception('用户名至少2个字符');
             }
             // 检查重复
             $user = UserAuthModel::getAuthByUsername($username);
             if ($user) {
                 throw new \Exception('用户名已被占用');
             }
             if (!$email) {
                 throw new \Exception('邮箱不能为空');
             }
             if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                 throw new \Exception('邮箱地址格式错误');
             }
             // 检查重复
             $user = UserAuthModel::getAuthByEmail($email);
             if ($user) {
                 throw new \Exception('邮箱已被占用');
             }
             if (!$mobile) {
                 throw new \Exception('手机号不能为空');
             }
             // 检查重复
             $user = UserAuthModel::getAuthByMobile($mobile);
             if ($user) {
                 throw new \Exception('手机号已被占用');
             }
             // 密码
             if (!$password) {
                 throw new \Exception('密码不能为空');
             }
             if (strlen($password) < 6) {
                 throw new \Exception('密码至少6位字符');
             }
             if (!$confirm_password) {
                 throw new \Exception('确认密码不能为空');
             }
             if ($password != $confirm_password) {
                 throw new \Exception('密码与确认密码不一致');
             }
             // 创建
             $auth = new UserAuthModel();
             $auth->username = $username;
             $auth->email = $email;
             $auth->mobile = $mobile;
             $auth->password = md5($password);
             $now = time();
             $auth->createTimestamp = $now;
             $auth->updateTimestamp = $now;
             // 后台添加的为可用状态
             $auth->status = UserAuthModel::STATUS_OK;
             // 保存
             UserAuthModel::createAuth($auth);
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_user_index'));
     }
     return $this->render('user/add.html.twig');
 }
Exemple #13
0
 /**
  * 保存
  * @param UserAuthModel $auth
  * @return UserAuthModel
  * @throws \Exception
  */
 public static function saveAuth(UserAuthModel $auth)
 {
     return self::editAuth($auth->toArray());
 }