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

miaoxing / plugin / 13066017523

26 Jan 2025 04:40AM UTC coverage: 41.349% (-1.1%) from 42.441%
13066017523

push

github

twinh
feat(plugin, experimental): 增加 `PresetColumns` 服务,用于生成常用的字段

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

147 existing lines in 4 files now uncovered.

1238 of 2994 relevant lines covered (41.35%)

36.64 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\Model\HasAppIdTrait;
7
use Miaoxing\Plugin\Model\ModelTrait;
8
use Miaoxing\Plugin\Model\ReqQueryTrait;
9
use Miaoxing\Plugin\Model\SnowflakeTrait;
10
use Wei\Model\CacheTrait;
11
use Wei\Password;
12
use Wei\V;
13

14
/**
15
 * @mixin \UserMixin
16
 * @mixin \AppMixin
17
 * @property string|null $id
18
 * @property string $appId
19
 * @property string $outId
20
 * @property int $adminType 管理员类型
21
 * @property string $groupId 用户组
22
 * @property bool $isAdmin
23
 * @property string $nickName
24
 * @property string $remarkName
25
 * @property string $username
26
 * @property string $name
27
 * @property string $email
28
 * @property string $mobile
29
 * @property string|null $mobileVerifiedAt 手机校验时间
30
 * @property string $phone
31
 * @property string $password
32
 * @property int $sex
33
 * @property string $country
34
 * @property string $province
35
 * @property string $city
36
 * @property string $district
37
 * @property string $address
38
 * @property string $signature
39
 * @property bool $isEnabled 是否启用
40
 * @property string $avatar
41
 * @property string|null $lastLoginAt
42
 * @property string|null $createdAt
43
 * @property string|null $updatedAt
44
 * @property string $createdBy
45
 * @property string $updatedBy
46
 * @property int $score 积分
47
 * @property string $money 账户余额
48
 * @property string $rechargeMoney 充值账户余额
49
 * @property string $source 用户来源
50
 * @property string|null $displayName
51
 */
52
class UserModel extends BaseModel
53
{
54
    use CacheTrait;
55
    use HasAppIdTrait;
56
    use ModelTrait;
57
    use ReqQueryTrait;
58
    use SnowflakeTrait;
59

60
    public const ADMIN_TYPE_DEFAULT = 1;
61

62
    public const ADMIN_TYPE_SUPER = 2;
63

64
    protected $hidden = [
65
        'password',
66
    ];
67

68
    protected $virtual = [
69
        'displayName',
70
    ];
71

72
    /**
73
     * @var array
74
     */
75
    protected $attributes = [
76
        'sex' => 1,
77
    ];
78

79
    public function getGuarded()
80
    {
81
        return array_merge($this->guarded, [
45✔
82
            'isAdmin',
45✔
83
            'mobileVerifiedAt',
45✔
84
            'username',
45✔
85
            'password',
45✔
86
            'lastLoginAt',
45✔
87
        ]);
45✔
88
    }
89

90
    /**
91
     * 设置未加密的密码
92
     *
93
     * @param string $password
94
     * @return $this
95
     */
96
    public function setPlainPassword($password)
97
    {
98
        $this->password = Password::hash($password);
10✔
99

100
        return $this;
10✔
101
    }
102

103
    /**
104
     * 验证密码是否正确
105
     *
106
     * @param string $password
107
     * @return bool
108
     */
109
    public function verifyPassword($password)
110
    {
111
        return Password::verify($password, $this->password);
5✔
112
    }
113

114
    /**
115
     * @return string|null
116
     */
117
    public function getDisplayNameAttribute()
118
    {
119
        foreach (['nickName', 'username', 'name'] as $column) {
10✔
120
            if ($name = $this[$column]) {
10✔
121
                return $name;
10✔
122
            }
123
        }
124
        return null;
5✔
125
    }
126

127
    /**
128
     * Model: 判断用户是否为超级管理员
129
     *
130
     * @return bool
131
     */
132
    public function isSuperAdmin()
133
    {
134
        return self::ADMIN_TYPE_SUPER === $this->adminType;
×
135
    }
136

137
    /**
138
     * 通过外部检查用户是否有某个权限
139
     *
140
     * @param string $permissionId
141
     * @return bool
142
     * @svc
143
     */
144
    protected function can($permissionId)
145
    {
UNCOV
146
        $result = $this->getEventService()->until('userCan', [$permissionId, $this]);
×
147
        if (null === $result) {
×
148
            $result = true;
×
149
        }
150

UNCOV
151
        return (bool) $result;
×
152
    }
153

154
    /**
155
     * @param array|\ArrayAccess $req
156
     * @return \Wei\Ret
157
     * @svc
158
     */
159
    protected function updatePassword($req)
160
    {
161
        // 1. 校验
UNCOV
162
        $v = V::defaultNotEmpty();
×
UNCOV
163
        $v->string('oldPassword', '旧密码', 6, 50);
×
164
        $v->string('password', '新密码')->when(wei()->user->enablePinCode, static function (V $v) {
×
UNCOV
165
            $v->digit()->length(6);
×
UNCOV
166
        }, static function (V $v) {
×
UNCOV
167
            $v->length(6, 50);
×
UNCOV
168
        });
×
UNCOV
169
        $v->string('passwordConfirm', '重复密码')->equalTo($req['password'])->message(
×
UNCOV
170
            'equalTo',
×
UNCOV
171
            '两次输入的密码不相等'
×
UNCOV
172
        );
×
UNCOV
173
        $ret = $v->check($req);
×
UNCOV
174
        if ($ret->isErr()) {
×
UNCOV
175
            return $ret;
×
176
        }
177

178
        // 2. 验证旧密码
UNCOV
179
        if ($this['password'] && !$this->verifyPassword($req['oldPassword'])) {
×
UNCOV
180
            return err('旧密码错误!请重新输入');
×
181
        }
182

183
        // 3. 更新新密码
UNCOV
184
        if (!$this->app->isDemo()) {
×
UNCOV
185
            $this->setPlainPassword($req['password']);
×
UNCOV
186
            $this->save();
×
187
        }
188

UNCOV
189
        User::logout();
×
190

UNCOV
191
        return suc();
×
192
    }
193

194
    protected function afterDestroy()
195
    {
UNCOV
196
        $this->removeModelCache();
×
197
    }
198

199
    protected function afterSave()
200
    {
201
        $this->removeModelCache();
45✔
202
        $this->user->refresh($this);
45✔
203
    }
204
}
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