Completed
Push — master ( 372919...09ca5f )
by Iman
13s
created

AuthController::validateForgotPass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\AuthModule;
4
5
use crocodicstudio\crudbooster\CBCoreModule\CbUsersRepo;
6
use crocodicstudio\crudbooster\controllers\Controller;
7
use crocodicstudio\crudbooster\helpers\Mailer;
8
use Illuminate\Support\Facades\Request;
9
use Illuminate\Support\Facades\Session;
10
use Illuminate\Support\Facades\Validator;
11
use CRUDBooster;
0 ignored issues
show
Bug introduced by
The type CRUDBooster was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use CB;
13
14
class AuthController extends Controller
15
{
16
    /**
17
     * @param null $tableName
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $tableName is correct as it would always require null to be passed?
Loading history...
18
     * @return mixed
19
     */
20
    public function table($tableName = null)
21
    {
22
        $tableName = $tableName ?: $this->table;
0 ignored issues
show
Bug Best Practice introduced by
The property table does not exist on crocodicstudio\crudboost...thModule\AuthController. Did you maybe forget to declare it?
Loading history...
23
        return \DB::table($tableName);
24
    }
25
26
    function getIndex()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
    {
28
        return view('CbAuth::home', ['page_title' => '<strong>Dashboard</strong>']);
29
    }
30
31
    public function getLockscreen()
32
    {
33
        if (! CRUDBooster::myId()) {
34
            Session::flush();
35
            return redirect()->route('getLogin')->with('message', cbTrans('alert_session_expired'));
36
        }
37
38
        Session::put('admin_lock', 1);
39
        return view('CbAuth::lockscreen');
40
    }
41
42
    public function postUnlockScreen()
43
    {
44
        $user = CbUsersRepo::find(CRUDBooster::myId());
45
46
        if (\Hash::check(request('password'), $user->password)) {
0 ignored issues
show
Bug introduced by
It seems like request('password') can also be of type array; however, parameter $value of Illuminate\Support\Facades\Hash::check() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
        if (\Hash::check(/** @scrutinizer ignore-type */ request('password'), $user->password)) {
Loading history...
47
            Session::put('admin_lock', 0);
48
49
            return redirect()->route('AuthControllerGetIndex');
50
        }
51
        echo "<script>alert('".cbTrans('alert_password_wrong')."');history.go(-1);</script>";
52
    }
53
54
    public function getLogin()
55
    {
56
        if (CRUDBooster::myId()) {
57
            return redirect(cbAdminPath());
58
        }
59
60
        return view('CbAuth::login');
61
    }
62
63
    public function getForgot()
64
    {
65
        if (CRUDBooster::myId()) {
66
            return redirect()->action('\\'.AuthController::class.'@getIndex');
67
        }
68
69
        return view('CbAuth::forgot');
70
    }
71
72
    public function postForgot()
73
    {
74
        $this->validateForgotPass();
75
76
        $randString = str_random(5);
77
        CbUsersRepo::updateByMail(request('email'), ['password' => \Hash::make($randString)]);
78
79
        //$appname = cbGetsetting('appname');
80
        $user = CbUsersRepo::findByMail(request('email'));
81
        $user->password = $randString;
82
        (new Mailer())->send(['to' => $user->email, 'data' => $user, 'template' => 'forgot_password_backend']);
83
84
        CRUDBooster::insertLog(cbTrans('log_forgot', ['email' => request('email'), 'ip' => Request::server('REMOTE_ADDR')]));
85
86
        return redirect()->route('getLogin')->with('message', cbTrans('message_forgot_password'));
87
    }
88
89
    public function getLogout()
90
    {
91
        CRUDBooster::insertLog(cbTrans('log_logout', ['email' => CRUDBooster::me()->email]));
92
        Session::flush();
93
94
        return redirect()->route('getLogin')->with('message', cbTrans('message_after_logout'));
95
    }
96
97
    private function validateForgotPass()
98
    {
99
        $validator = Validator::make(request()->all(), ['email' => 'required|email|exists:cms_users',]);
100
101
        if ($validator->fails()) {
102
            $message = $validator->errors()->all();
103
            backWithMsg(implode(', ', $message), 'danger');
104
        }
105
    }
106
}
107