• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

miaoxing / admin / 5972842237

25 Aug 2023 06:45AM UTC coverage: 28.924%. First build
5972842237

push

github

twinh
fix(admin, u): 用户名不允许以数字开头

使用数字开头会和手机号冲突

22 of 85 branches covered (0.0%)

0 of 2 new or added lines in 1 file covered. (0.0%)

164 of 567 relevant lines covered (28.92%)

2.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pages/api/admin/admins/[id].php
1
<?php
2

3
use Miaoxing\Admin\Service\AdminModel;
4
use Miaoxing\App\Service\RolesUserModel;
5
use Miaoxing\Plugin\BasePage;
6
use Miaoxing\Plugin\Service\UserModel;
7
use Miaoxing\Services\Page\ItemTrait;
8
use Miaoxing\Services\Service\ShowAction;
9
use Wei\Req;
10
use Wei\V;
11

12
return new class () extends BasePage {
13
    use ItemTrait;
14

15
    protected $className = '管理员';
16

17
    protected $include = [
18
        'user',
19
    ];
20

21
    public function get()
22
    {
23
        return ShowAction::new()
×
24
            ->buildData(function (AdminModel $admin) {
25
                $userRoles = RolesUserModel::fetchAll('user_id', $admin->userId);
×
26
                return [
×
27
                    'roleIds' => array_column($userRoles, 'roleId'),
×
28
                ];
×
29
            })
×
30
            ->exec($this);
×
31
    }
32

33
    public function patch(Req $req)
34
    {
35
        $data = (array) $req['user'];
×
36
        $admin = AdminModel::findFromReq();
×
37
        $user = $admin->user()->findOrInitBy();
×
38

39
        // 添加用户,编辑用户时,提交了密码,才检验密码是否合法
40
        $validatePassword = $user->isNew() || ($data['password'] ?? false);
×
41

42
        $v = V::new();
×
43
        $v->setModel($user);
×
NEW
44
        $v->alnum('username', '用户名')->required($user->isNew())->length(3, 32)->notModelDup()
×
NEW
45
            ->notRegex('/^\d/')->message('%name%不能以数字开头');
×
46
        $v->string('password', '密码', 6, 50)->required($validatePassword);
×
47
        $v->string('passwordAgain', '重复密码', 6, 50)->required($validatePassword)
×
48
            ->equalTo($data['password'] ?? null)->message('equalTo', '两次输入的密码不相等');
×
49
        $v->maxCharLength('nickName', ' 昵称', 32)->optional();
×
50
        $ret = $v->check($data);
×
51
        $this->tie($ret);
×
52

53
        if (isset($data['isEnabled']) && !$data['isEnabled'] && $user->isSuperAdmin()) {
×
54
            return err('不能禁用超级管理员');
×
55
        }
56

57
        // 只有校验过才存储到用户对象中
58
        if (isset($data['username'])) {
×
59
            $user->username = $data['username'];
×
60
        }
61

62
        if ($this->shouldUpdatePassword($validatePassword, $user)) {
×
63
            $user->setPlainPassword($data['password']);
×
64
        }
65

66
        $user->isAdmin = true;
×
67
        $user->save($data);
×
68
        $admin->save(['userId' => $user->id]);
×
69

70
        // 保存角色
71
        if (isset($req['roleIds'])) {
×
72
            $user->roles()->syncRelation($req['roleIds']);
×
73
        }
74

75
        return $admin->toRet();
×
76
    }
77

78
    protected function shouldUpdatePassword(bool $validatePassword, UserModel $user): bool
79
    {
80
        if ($user->isNew()) {
×
81
            // 新用户总是要更新密码
82
            return true;
×
83
        }
84

85
        if ($this->app->isDemo()) {
×
86
            // 演示模式下,编辑用户不更新密码
87
            return false;
×
88
        }
89

90
        // 提交了密码,需更新密码
91
        return $validatePassword;
×
92
    }
93
};
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc