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

miaoxing / plugin / 7322930040

25 Dec 2023 04:02PM UTC coverage: 37.918% (-1.7%) from 39.661%
7322930040

push

github

twinh
ci: add PHP 8, remove PHP 7.2, 7.3

907 of 2392 relevant lines covered (37.92%)

5.95 hits per line

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

37.21
/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
    public const ADMIN_TYPE_DEFAULT = 1;
29

30
    public const ADMIN_TYPE_SUPER = 2;
31

32
    protected $hidden = [
33
        'password',
34
    ];
35

36
    protected $virtual = [
37
        'displayName',
38
    ];
39

40
    /**
41
     * @var array
42
     */
43
    protected $attributes = [
44
        'sex' => 1,
45
    ];
46

47
    public function getGuarded()
48
    {
49
        return array_merge($this->guarded, [
9✔
50
            'isAdmin',
9✔
51
            'mobileVerifiedAt',
9✔
52
            'username',
9✔
53
            'password',
9✔
54
            'lastLoginAt',
9✔
55
        ]);
9✔
56
    }
57

58
    /**
59
     * 设置未加密的密码
60
     *
61
     * @param string $password
62
     * @return $this
63
     */
64
    public function setPlainPassword($password)
65
    {
66
        $this->password = Password::hash($password);
2✔
67

68
        return $this;
2✔
69
    }
70

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

82
    /**
83
     * @return string|null
84
     */
85
    public function getDisplayNameAttribute()
86
    {
87
        foreach (['nickName', 'username', 'name'] as $column) {
2✔
88
            if ($name = $this[$column]) {
2✔
89
                return $name;
2✔
90
            }
91
        }
92
        return null;
1✔
93
    }
94

95
    /**
96
     * Model: 判断用户是否为超级管理员
97
     *
98
     * @return bool
99
     */
100
    public function isSuperAdmin()
101
    {
102
        return self::ADMIN_TYPE_SUPER === $this->adminType;
×
103
    }
104

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

119
        return (bool) $result;
×
120
    }
121

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

146
        // 2. 验证旧密码
147
        if ($this['password'] && !$this->verifyPassword($req['oldPassword'])) {
×
148
            return err('旧密码错误!请重新输入');
×
149
        }
150

151
        // 3. 更新新密码
152
        if (!$this->app->isDemo()) {
×
153
            $this->setPlainPassword($req['password']);
×
154
            $this->save();
×
155
        }
156

157
        User::logout();
×
158

159
        return suc();
×
160
    }
161

162
    protected function afterDestroy()
163
    {
164
        $this->removeModelCache();
×
165
    }
166

167
    protected function afterSave()
168
    {
169
        $this->removeModelCache();
9✔
170
        $this->user->refresh($this);
9✔
171
    }
172
}
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