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

miaoxing / plugin / 3766633246

pending completion
3766633246

push

github

twinh
feat(plugin): `BasePage` 增加 `pageInit` 事件

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

84 existing lines in 24 files now uncovered.

902 of 2275 relevant lines covered (39.65%)

19.29 hits per line

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

33.33
/src/Service/UserModel.php
1
<?php
2

3
namespace Miaoxing\Plugin\Service;
4

5
use Miaoxing\Plugin\BaseModel;
6
use Miaoxing\Plugin\Metadata\UserTrait;
7
use Miaoxing\Plugin\Model\HasAppIdTrait;
8
use Miaoxing\Plugin\Model\ModelTrait;
9
use Miaoxing\Plugin\Model\ReqQueryTrait;
10
use Miaoxing\Plugin\Model\SnowflakeTrait;
11
use Wei\Model\CacheTrait;
12
use Wei\Password;
13
use Wei\V;
14

15
/**
16
 * @mixin \UserMixin
17
 * @mixin \AppMixin
18
 */
19
class UserModel extends BaseModel
20
{
21
    use CacheTrait;
22
    use HasAppIdTrait;
23
    use ModelTrait;
24
    use ReqQueryTrait;
25
    use SnowflakeTrait;
26
    use UserTrait;
27

28
    protected $hidden = [
29
        'password',
30
    ];
31

32
    protected $virtual = [
33
        'displayName',
34
    ];
35

36
    /**
37
     * @var array
38
     */
39
    protected $attributes = [
40
        'sex' => 1,
41
    ];
42

43
    public function getGuarded()
44
    {
45
        return array_merge($this->guarded, [
24✔
46
            'isAdmin',
24✔
47
            'mobileVerifiedAt',
16✔
48
            'username',
16✔
49
            'password',
16✔
50
            'lastLoginAt',
16✔
51
        ]);
16✔
52
    }
53

54
    /**
55
     * 设置未加密的密码
56
     *
57
     * @param string $password
58
     * @return $this
59
     */
60
    public function setPlainPassword($password)
61
    {
62
        $this->password = Password::hash($password);
6✔
63

64
        return $this;
6✔
65
    }
66

67
    /**
68
     * 验证密码是否正确
69
     *
70
     * @param string $password
71
     * @return bool
72
     */
73
    public function verifyPassword($password)
74
    {
75
        return Password::verify($password, $this->password);
3✔
76
    }
77

78
    /**
79
     * @return string|null
80
     */
81
    public function getDisplayNameAttribute()
82
    {
83
        foreach (['nickName', 'username', 'name'] as $column) {
6✔
84
            if ($name = $this[$column]) {
6✔
85
                return $name;
6✔
86
            }
87
        }
88
        return null;
3✔
89
    }
90

91
    /**
92
     * Model: 判断用户是否为超级管理员
93
     *
94
     * @return bool
95
     */
96
    public function isSuperAdmin()
97
    {
98
        return '1' === $this->id;
×
99
    }
100

101
    /**
102
     * 通过外部检查用户是否有某个权限
103
     *
104
     * @param string $permissionId
105
     * @return bool
106
     * @svc
107
     */
108
    protected function can($permissionId)
109
    {
110
        $result = $this->getEventService()->until('userCan', [$permissionId, $this]);
×
111
        if (null === $result) {
×
112
            $result = true;
×
113
        }
114

115
        return (bool) $result;
×
116
    }
117

118
    /**
119
     * @param array|\ArrayAccess $req
120
     * @return \Wei\Ret
121
     * @svc
122
     */
123
    protected function updatePassword($req)
124
    {
125
        // 1. 校验
126
        $v = V::defaultNotEmpty();
×
127
        $v->string('oldPassword', '旧密码', 6, 50);
×
128
        $v->string('password', '新密码')->when(wei()->user->enablePinCode, static function (V $v) {
129
            $v->digit()->length(6);
×
130
        }, static function (V $v) {
131
            $v->length(6, 50);
×
UNCOV
132
        });
×
133
        $v->string('passwordConfirm', '重复密码')->equalTo($req['password'])->message('equalTo', '两次输入的密码不相等');
×
134
        $ret = $v->check($req);
×
135
        if ($ret->isErr()) {
×
136
            return $ret;
×
137
        }
138

139
        // 2. 验证旧密码
140
        if ($this['password'] && !$this->verifyPassword($req['oldPassword'])) {
×
141
            return err('旧密码错误!请重新输入');
×
142
        }
143

144
        // 3. 更新新密码
145
        if (!$this->app->isDemo()) {
×
146
            $this->setPlainPassword($req['password']);
×
147
            $this->save();
×
148
        }
149

150
        User::logout();
×
151

152
        return suc();
×
153
    }
154

155
    protected function afterDestroy()
156
    {
157
        $this->removeModelCache();
×
158
    }
159

160
    protected function afterSave()
161
    {
162
        $this->removeModelCache();
24✔
163
        $this->user->refresh($this);
24✔
164
    }
8✔
165
}
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