Beispiel #1
0
 public static function do_login_action()
 {
     // 拦截
     self::method('post');
     try {
         // 校验
         $checker = new lazy_checker(p());
         $checker->check('name', array('change_to' => 'string', 'cannot_be' => array('', '用户不能为空')));
         $checker->check('pass', array('change_to' => 'string', 'cannot_be' => array('', '密码不能为空')));
         // 执行
         $member = member_model::get_one(array('name' => $checker->name));
         if ($member === null) {
             $checker->failed('name', '用户名不正确');
         }
         if (!$member->is_valid_pass($checker->pass)) {
             $checker->failed('pass', '密码不正确');
         }
         visitor::set_role('member', $member->id, p_has('remember') ? 30 * 86400 : 0, []);
         // 成功
         self::json_result(true, '登录成功', 0, url('site/admin'));
     } catch (check_failed $e) {
         // 失败
         self::json_result(false, $e->get_reasons());
     }
 }
Beispiel #2
0
 public static function new_action()
 {
     // 拦截
     self::method('post');
     $post_id = g_int('post_id');
     $post = post_model::get_by_id($post_id);
     self::forward_404_if($post === null, '文章不存在,无法评论');
     try {
         // 校验
         $checker = new lazy_checker(p());
         $checker->check('captcha', array('should_be' => array(setting_model::get_by_id(setting_model::id_captcha_answer)->value, '验证码不正确')));
         $checker->del('captcha');
         $checker->check_model_rules('comment');
         $comment = $checker->get_all();
         if (!visitor::has_role('member') && member_model::get_one(array('name' => $comment['author'])) !== null) {
             $checker->failed('author', '您不能使用管理员的昵称');
         }
         // 执行
         $comment['post_id'] = $post_id;
         $comment['pub_time'] = clock::get_stamp();
         comment_model::add($comment);
         post_model::inc_by_id(array('comment_count' => 1), $post_id);
         setting_model::inc_by_id(array('value' => 1), setting_model::id_comment_count);
         // 成功
         self::json_result(true, '评论成功', 0, url('post/show?id=' . $post_id));
     } catch (check_failed $e) {
         // 失败
         self::json_result(false, $e->get_reasons());
     }
 }
Beispiel #3
0
 public static function prop_rules($for_operation, $extra_data)
 {
     $member = $extra_data;
     $rules = array('name' => array('change_to' => 'string', 'char_size' => array(3, self::name_len, '昵称长度:%d-%d'), 'callback' => $for_operation === 'new' ? function ($name) {
         if (member_model::get_one(array('name' => $name)) !== null) {
             return '该用户已经存在';
         }
     } : function ($name) use($member) {
         if ($name !== $member->name && member_model::get_one(array('name' => $name)) !== null) {
             return '该用户已经存在';
         }
     }), 'pass' => array('change_to' => 'string', 'char_size' => array(6, -1, '密码不能小于%d个字符')), 'repass' => array('same_as' => array('pass', '两次输入的密码不相同')));
     if ($for_operation === 'edit') {
         $rules['pass']['pass_if_be'] = '';
     }
     return $rules;
 }