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

miaoxing / user / 6817969812

02 Nov 2023 02:41PM UTC coverage: 15.319%. Remained the same
6817969812

push

github

semantic-release-bot
chore(release): publish

See CHANGELOG.md for more details.

21 of 80 branches covered (0.0%)

108 of 705 relevant lines covered (15.32%)

1.54 hits per line

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

0.0
/src/Controller/RegistrationController.php
1
<?php
2

3
namespace Miaoxing\User\Controller;
4

5
use Miaoxing\Plugin\BaseController;
6
use Miaoxing\Plugin\Service\User;
7
use Miaoxing\User\Mailer\Register;
8
use Miaoxing\User\Middleware\CheckNotLogin;
9
use Miaoxing\User\Middleware\CheckNotVerified;
10
use Miaoxing\User\Middleware\CheckVerified;
11

12
class RegistrationController extends BaseController
13
{
14
    protected $actionAuths = [
15
        'register' => false,
16
        'create' => false,
17
        'forget' => false,
18
        'reset' => false,
19
        'verify' => false,
20
    ];
21

22
    public function init()
23
    {
24
        parent::init();
×
25

26
        $this->middleware(CheckNotLogin::class, [
×
27
            'only' => ['register', 'create', 'forget', 'reset'],
×
28
            'redirect' => 'admin',
×
29
        ]);
×
30

31
        $this->middleware(CheckVerified::class, [
×
32
            'except' => [
×
33
                'editEmail',
×
34
                'updateEmail',
×
35
                'resendEmail',
×
36
                'confirm',
×
37
                'verify',
×
38
            ],
×
39
        ]);
×
40

41
        $this->middleware(CheckNotVerified::class, [
×
42
            'only' => [
×
43
                'editEmail',
×
44
                'updateEmail',
×
45
                'resendEmail',
×
46
                'createResetByEmail',
×
47
                'resetUpdate',
×
48
            ],
×
49
        ]);
×
50
    }
51

52
    public function registerAction($req)
53
    {
54
        $agreementArticleId = wei()->user->agreementArticleId;
×
55

56
        return get_defined_vars();
×
57
    }
58

59
    public function forgetAction($req)
60
    {
61
        return get_defined_vars();
×
62
    }
63

64
    public function createResetByEmailAction($req)
65
    {
66
        $ret = wei()->captcha->check($req['captcha']);
×
67
        if (1 !== $ret['code']) {
×
68
            $ret['captchaErr'] = true;
×
69

70
            return $this->ret($ret);
×
71
        }
72

73
        $ret = wei()->userPassword->createResetByEmail($req);
×
74

75
        return $this->ret($ret);
×
76
    }
77

78
    /**
79
     * 重置密码页面
80
     * @param $req
81
     * @return array
82
     */
83
    public function resetAction($req)
84
    {
85
        $this->page->setTitle('重置密码');
×
86
        $ret = wei()->userVerify->verify($req, false);
×
87
        if ($ret['code'] < 0) {
×
88
            return $this->err($ret['message']);
×
89
        }
90

91
        $userId = wei()->e($req['userId']);
×
92
        $nonce = wei()->e($req['nonce']);
×
93
        $timestamp = wei()->e($req['timestamp']);
×
94
        $sign = wei()->e($req['sign']);
×
95

96
        return get_defined_vars();
×
97
    }
98

99
    /**
100
     * 重置密码更新数据库
101
     * @param $req
102
     * @return \Wei\Res
103
     */
104
    public function resetUpdateAction($req)
105
    {
106
        $ret = wei()->userPassword->resetPassword($req);
×
107

108
        return $this->ret($ret);
×
109
    }
110

111
    public function createAction($req)
112
    {
113
        // 1. 增加注册的校验
114
        $this->event->on('userRegisterValidate', function () use ($req) {
115
            $ret = wei()->captcha->check($req['captcha']);
×
116
            if (1 !== $ret['code']) {
×
117
                $ret['captchaErr'] = true;
×
118

119
                return $ret;
×
120
            }
121

122
            if (wei()->user->agreementArticleId && !$req['agreement']) {
×
123
                return $this->err('请同意《服务协议》');
×
124
            }
125
        });
×
126

127
        // 2. 调用注册接口
128
        $user = wei()->user();
×
129
        $ret = $user->register($req);
×
130
        if (1 !== $ret['code']) {
×
131
            return $ret;
×
132
        }
133

134
        // 3. 设置用户角色为管理员
135
        $user->save([
×
136
            'admin' => true,
×
137
            'isValid' => false, // 待验证邮箱
×
138
        ]);
×
139

140
        wei()->event->trigger('postAdminRegister', [$user]);
×
141

142
        // 4. 登录用户
143
        $loginRet = User::loginById($user['id']);
×
144
        if (1 !== $loginRet['code']) {
×
145
            return $loginRet;
×
146
        }
147

148
        // 5. 发送确认邮件
149
        $sendRet = wei()->mail->send(Register::class, [
×
150
            'user' => $user,
×
151
        ]);
×
152
        if (1 !== $sendRet['code']) {
×
153
            return $sendRet;
×
154
        }
155

156
        return $ret;
×
157
    }
158

159
    public function confirmAction($req)
160
    {
161
        return get_defined_vars();
×
162
    }
163

164
    public function verifyAction($req)
165
    {
166
        $ret = wei()->userVerify->verify($req);
×
167
        if (1 !== $ret['code']) {
×
168
            return $ret;
×
169
        }
170

171
        $user = $ret['user'];
×
172
        $user->save(['isValid' => true]);
×
173

174
        return get_defined_vars();
×
175
    }
176

177
    public function resendEmailAction()
178
    {
179
        $ret = wei()->mail->send(Register::class, [
×
180
            'user' => User::cur(),
×
181
        ]);
×
182

183
        return $ret;
×
184
    }
185

186
    public function editEmailAction()
187
    {
188
        return get_defined_vars();
×
189
    }
190

191
    public function updateEmailAction($req)
192
    {
193
        $validator = wei()->validate([
×
194
            'data' => $req,
×
195
            'rules' => [
×
196
                'email' => [
×
197
                    'email' => true,
×
198
                    'notEqualTo' => User::cur()->email,
×
199
                    'notRecordExists' => ['user', 'email'],
×
200
                ],
×
201
            ],
×
202
            'names' => [
×
203
                'email' => '新邮箱',
×
204
            ],
×
205
            'messages' => [
×
206
                'email' => [
×
207
                    'notEqualTo' => '新邮箱不能和原邮箱相同',
×
208
                ],
×
209
            ],
×
210
        ]);
×
211
        if (!$validator->isValid()) {
×
212
            return $this->err($validator->getFirstMessage());
×
213
        }
214

215
        User::cur()->save([
×
216
            'email' => $req['email'],
×
217
        ]);
×
218

219
        $ret = wei()->mail->send(Register::class, [
×
220
            'user' => User::cur(),
×
221
        ]);
×
222

223
        return $ret;
×
224
    }
225
}
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