UserController.php 3.33 KB
Newer Older
ZeMKI's avatar
ZeMKI committed
1
2
3
4
<?php

namespace App\Http\Controllers;

ZeMKI's avatar
ZeMKI committed
5
6
7
use App\Mail\RegisterSupervisor;
use App\Mail\VerificationEmail;
use App\Permission;
8
9
use App\Role;
use App\Study;
ZeMKI's avatar
ZeMKI committed
10
11
12
13
14
15
16
17
18
19
use App\User;
use Auth;
use DB;
use Helper;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Contracts\View\Factory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\RedirectResponse;
ZeMKI's avatar
ZeMKI committed
20
use Illuminate\Http\Request;
21
use Illuminate\Http\Response;
Alessandro Belli's avatar
Alessandro Belli committed
22
use Illuminate\Routing\Redirector;
ZeMKI's avatar
ZeMKI committed
23
use Illuminate\Support\Facades\Date;
ZeMKI's avatar
ZeMKI committed
24
use Illuminate\Support\Facades\Mail;
ZeMKI's avatar
ZeMKI committed
25
use Illuminate\View\View;
ZeMKI's avatar
ZeMKI committed
26
27
28
29

class UserController extends Controller
{
    /**
30
     * Get data to fill select dropdown input.
ZeMKI's avatar
ZeMKI committed
31
32
     * @return [collection] [input data for the form]
     */
Laravel Shift's avatar
Laravel Shift committed
33
34
    public function getinputdata()
    {
ZeMKI's avatar
ZeMKI committed
35
        $allRoles = [['value' => 2, 'name' => 'Supervisor']];
36
37
38
39
40
41
42
43
        // $relatedstudies = Auth::user()->studiesforedit;
        // $data['relatedstudies'] = $relatedstudies;
        // if(in_array(1,Auth::user()->roles()->pluck('role_id')->toArray())){
        //   $data['supervisors'] = User::join('user_roles', 'users.id', '=','user_roles.user_id')->where('role_id','=','2')->get();
        //   $allRoles = [['value' => 1,'name' => 'Admin'],['value' => 2,'name' => 'Supervisor'],['value' => 3,'name' => 'Researcher']];
        // }
        $data['allroles'] = $allRoles;
        return response()->json($data, 200);
ZeMKI's avatar
ZeMKI committed
44
45
46
    }


ZeMKI's avatar
ZeMKI committed
47
    /**
48
     * Register a user.
ZeMKI's avatar
ZeMKI committed
49
     * @param User   $user
50
     * @param string $text
51
     * @return void
ZeMKI's avatar
ZeMKI committed
52
53
54
55
56
57
     */
    public function sendEmail(User $user, $text)
    {
        Mail::to($user)->send(new RegisterSupervisor($text));
    }

ZeMKI's avatar
ZeMKI committed
58
59
    /**
     * Display a listing of the resource.
60
     * @return void
ZeMKI's avatar
ZeMKI committed
61
62
63
64
65
66
67
     */
    public function index()
    {
    }

    /**
     * Show the form for creating a new resource.
68
     * @return void
ZeMKI's avatar
ZeMKI committed
69
70
71
72
73
     */
    public function create()
    {
    }

ZeMKI's avatar
ZeMKI committed
74

ZeMKI's avatar
ZeMKI committed
75
76
77
78
79
80
81
82
83
    /**
     * @param Request $request
     * @return bool
     */
    private function sendTestEmail(Request $request): bool
    {
        if ($request->testEmail == true) {
            Mail::to($request->email)->send(new VerificationEmail(new User(), $request->emailtext ? $request->emailtext : config('utilities.emailDefaultText')));
            return true;
84
        }
ZeMKI's avatar
ZeMKI committed
85
        return false;
ZeMKI's avatar
ZeMKI committed
86
    }
ZeMKI's avatar
ZeMKI committed
87
88
89

    /**
     * Show the form for editing the specified resource.
90
     * @param int $id
ZeMKI's avatar
ZeMKI committed
91
     * @return User|Builder|Collection|Model|null
ZeMKI's avatar
ZeMKI committed
92
93
94
     */
    public function edit($id)
    {
95
        return User::with('studies', 'roles')->find($id);
ZeMKI's avatar
ZeMKI committed
96
97
98
99
    }

    /**
     * Remove the specified resource from storage.
100
101
     * @param int $id
     * @return Response
102
     * @throws \Exception
ZeMKI's avatar
ZeMKI committed
103
     */
ZeMKI's avatar
ZeMKI committed
104
    public function destroy(User $user)
ZeMKI's avatar
ZeMKI committed
105
    {
106
107
        if(!auth()->user()->isAdmin())abort(401);
        //$user = User::where('id', $id)->first();
108
        $user->delete();
109
        return response('user deleted', 200);
ZeMKI's avatar
ZeMKI committed
110
111
    }

ZeMKI's avatar
ZeMKI committed
112

Alessandro Belli's avatar
Alessandro Belli committed
113
114
    /**
     * @param User $user
ZeMKI's avatar
ZeMKI committed
115
     * @return ResponseFactory|Response
Alessandro Belli's avatar
Alessandro Belli committed
116
     */
ZeMKI's avatar
ZeMKI committed
117
    public function sendresetpassword(User $user)
Laravel Shift's avatar
Laravel Shift committed
118
    {
119
120
121
122
        $user->password = Helper::random_str(60);
        $user->password_token = Helper::random_str(30);
        Mail::to($user->email)->send(new VerificationEmail($user));
        $user->save();
ZeMKI's avatar
ZeMKI committed
123
        return response('Password is resetted for ' . $user->email . ' and an email was sent.');
ZeMKI's avatar
ZeMKI committed
124
125
    }

126
}