Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Apps/Controller/Front/User/ActionSignup.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Apps\Controller\Front\User;
4
5
use Apps\ActiveRecord\Invite;
6
use Apps\Model\Front\User\FormLogin;
7
use Apps\Model\Front\User\FormRegister;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Arch\View;
10
use Ffcms\Core\Exception\ForbiddenException;
11
use Ffcms\Core\Helper\Type\Str;
12
use Ffcms\Core\Network\Request;
13
use Ffcms\Core\Network\Response;
14
15
/**
16
 * Trait ActionSignup
17
 * @package Apps\Controller\Front\User
18
 * @property View $view
19
 * @property Response $response
20
 * @property Request $request
21
 * @method array getConfigs()
22
 */
23
trait ActionSignup
24
{
25
26
    /**
27
     * View register form and process submit action
28
     * @throws ForbiddenException
29
     * @throws \Ffcms\Core\Exception\SyntaxException
30
     */
31
    public function signup(): ?string
32
    {
33
        // check if user is authorized
34
        if (App::$User->isAuth()) {
35
            throw new ForbiddenException(__('You are always authorized on website, registration not allowed'));
36
        }
37
38
        // load configs
39
        $configs = $this->getConfigs();
40
41
        // init register model
42
        $registerForm = new FormRegister($configs['captchaOnRegister'] === 1);
43
44
        // registration based on invite. Check conditions.
45
        if ($configs['registrationType'] === 0) {
46
            // get token and email
47
            $inviteToken = $this->request->query->get('token');
48
            $inviteEmail = $this->request->query->get('email');
49
            // check if token length & email is valid format
50
            if (Str::length($inviteToken) < 32 || !Str::isEmail($inviteEmail)) {
51
                throw new ForbiddenException(__('Registration allowed only if you have invite!'));
52
            }
53
54
            // remove deprecated data
55
            Invite::clean();
56
            // try to find token
57
            $find = Invite::where('token', $inviteToken)
58
                ->where('email', $inviteEmail)
59
                ->count();
60
61
            // token not foud? invalid invite key
62
            if ($find !== 1) {
63
                throw new ForbiddenException(__('Your invite token is invalid! Contact with administrator'));
64
            }
65
66
            // notify the invite token is accepted
67
            if (!$registerForm->send()) {
68
                App::$Session->getFlashBag()->add('success', __('Invite was accepted! Continue registration'));
69
            }
70
71
            // set email from token data
72
            $registerForm->email = $inviteEmail;
73
        }
74
75
        // if register data is send and valid
76
        if ($registerForm->send() && $registerForm->validate()) {
77
            $activation = $configs['registrationType'] === 1;
78
            if ($registerForm->tryRegister($activation)) {
79
                // initialize succes signup event
80
                App::$Event->run(static::EVENT_USER_REGISTER_SUCCESS, [
0 ignored issues
show
The constant Apps\Controller\Front\Us...T_USER_REGISTER_SUCCESS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
81
                    'model' => $registerForm
82
                ]);
83
                // if no activation is required - just open session and redirect user to main page
84
                if (!$activation) {
85
                    $loginModel = new FormLogin();
86
                    $loginModel->openSession($registerForm->_userObject);
87
                    $this->response->redirect('/'); // session is opened, refresh page
88
                }
89
                // send notification of successful registering
90
                App::$Session->getFlashBag()->add('success', __('Your account is registered. You must confirm account via email'));
91
            } else {
92
                // init fail signup event
93
                App::$Event->run(static::EVENT_USER_REGISTER_FAIL, [
0 ignored issues
show
The constant Apps\Controller\Front\Us...VENT_USER_REGISTER_FAIL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
94
                    'model' => $registerForm
95
                ]);
96
                App::$Session->getFlashBag()->add('error', __('Login or email is always used on website'));
97
            }
98
        }
99
100
        // render view
101
        return $this->view->render('user/signup', [
102
            'model' => $registerForm,
103
            'config' => $configs,
104
            'useCaptcha' => $configs['captchaOnRegister'] === 1
105
        ]);
106
    }
107
}
108