Passed
Push — dev5 ( 125e24...928bc2 )
by Ron
09:11
created

SetUserDetails::disableUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 13
ccs 7
cts 8
cp 0.875
crap 2.0078
rs 10
1
<?php
2
3
namespace App\Domains\Users;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Facades\Log;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Notification;
9
10
use App\Http\Requests\UserCreateRequest;
11
use App\Http\Requests\UserBasicAccountRequest;
12
use App\Http\Requests\UserNotificationSettingsRequest;
13
14
use App\Notifications\NewUserEmail;
15
16
use Carbon\Carbon;
17
18
use App\User;
19
use App\UserSettings;
20
use App\UserInitialize;
21
22
class SetUserDetails
23
{
24
    protected $userID;
25
26
    //  Create a new user
27 2
    public function createNewUser(UserCreateRequest $request)
28
    {
29
        //  Create the user
30 2
        $newUser = User::create([
31 2
            'role_id'          => $request->role_id,
32 2
            'username'         => $request->username,
33 2
            'first_name'       => $request->first_name,
34 2
            'last_name'        => $request->last_name,
35 2
            'email'            => $request->email,
36 2
            'password'         => bcrypt(strtolower(Str::random(15))),
37 2
            'password_expires' => Carbon::now()->subDay(),
38
        ]);
39 2
        $userID = $newUser->user_id;
40 2
        Log::notice('New User created by '.Auth::user()->full_name.'.  Data - ', $newUser->toArray());
41
42
        //  Create the user settings table
43 2
        UserSettings::create([
44 2
            'user_id' => $userID,
45
        ]);
46
47
        //  Create the setup user link
48 2
        $hash = strtolower(Str::random(30));
49 2
        UserInitialize::create([
50 2
            'username' => $request->username,
51 2
            'token'    => $hash
52
        ]);
53 2
        Log::info('User Initialize link created for User ID '.$userID.'. New Link Hash - '.$hash);
54
55
        //  Email the new user
56 2
        Notification::send($newUser, new NewUserEmail($newUser, $hash));
57
58 2
        return $userID;
59
    }
60
61
    //  Disable a user
62 2
    public function disableUser($userID)
63
    {
64 2
        $userData = User::find($userID);
65 2
        if($userData->role_id < Auth::user()->role_id)
66
        {
67
            abort(403, 'You cannot disable a user with higher permissions than you');
68
        }
69
70 2
        $user = User::find($userID);
71 2
        Log::alert('User '.$user->full_name.' has been deactivated by '.Auth::user()->full_name.'.  User Details - ', array($user));
72 2
        $user->delete();
73
74 2
        return true;
75
    }
76
77
    //  Update the user's basic settings
78 4
    public function updateUserDetails(UserBasicAccountRequest $request, $userID = null)
79
    {
80 4
        if(!$userID)
81
        {
82 4
            $userID = Auth::user()->user_id;
83
        }
84
85 4
        if(isset($request->username))
86
        {
87 2
            $details = User::find($userID)->update(
88
            [
89 2
                'username'   => $request->username,
90 2
                'role_id'    => $request->role_id,
91 2
                'first_name' => $request->first_name,
92 2
                'last_name'  => $request->last_name,
93 2
                'email'      => $request->email
94
            ]);
95
        }
96
        else
97
        {
98 2
            $details = User::find($userID)->update(
99
            [
100 2
                'first_name' => $request->first_name,
101 2
                'last_name'  => $request->last_name,
102 2
                'email'      => $request->email
103
            ]);
104
        }
105
106 4
        Log::notice('User ID '.$userID.' has been updated by '.Auth::user()->full_name.'.  Details - ', array($details));
107 4
        return true;
108
    }
109
110
    //  Update the user's email notification settings
111 4
    public function updateUserNotifications(UserNotificationSettingsRequest $request, $userID = null)
112
    {
113 4
        if(!$userID)
114
        {
115 2
            $userID = Auth::user()->user_id;
116
        }
117
118 4
        $details = UserSettings::where('user_id', Auth::user()->user_id)->update(
119
        [
120 4
            'em_tech_tip'     => $request->em_tech_tip,
121 4
            'em_file_link'    => $request->em_file_link,
122 4
            'em_notification' => $request->em_notification,
123 4
            'auto_del_link'   => $request->auto_del_link,
124
        ]);
125
126 4
        Log::notice('User ID '.$userID.' notification settings have been updated by '.Auth::user()->full_name.'.  Details - ', array($details));
127 4
        return true;
128
    }
129
130
    //  Update the user's password
131 6
    public function updateUserPassword($password, $userID = null, $forceChange = false)
132
    {
133 6
        if(!$userID)
134
        {
135 2
            $userID = Auth::user()->user_id;
136
        }
137
        else
138
        {
139
            //  Verify that the user trying to change the password does not have less permissions than the user being reset
140 4
            $userData = User::find($userID);
141 4
            if($userData->role_id < Auth::user()->role_id)
142
            {
143
                abort(403, 'Unable to Reset Password for user with more permissions than you');
144
            }
145
        }
146
147
        //  Determine if there is a new password expire's date
148 6
        if(!$forceChange)
149
        {
150 4
            $newExpire = config('auth.passwords.settings.expire') != null ?
151 4
               Carbon::now()->addDays(config('auth.passwords.settings.expire')) : null;
152
        }
153
        else
154
        {
155 2
            $newExpire = Carbon::now()->subDay();
156
        }
157
158 6
        User::find($userID)->update([
159 6
            'password'         => bcrypt($password),
160 6
            'password_expires' => $newExpire,
161
        ]);
162
163 6
        Log::notice('Password has been changed for User ID '.$userID.' by '.Auth::user()->full_name);
164 6
        return true;
165
    }
166
}
167