Completed
Push — master ( 2ff197...58043b )
by Sergi Tur
06:57
created

UsersManagementController::massiveDestroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Acacha\Users\Http\Controllers;
4
5
use Acacha\Users\Events\UserCreated;
6
use Acacha\Users\Http\Requests\CreateUserRequest;
7
use Acacha\Users\Http\Requests\MassiveDestroyRequest;
8
use Acacha\Users\Http\Requests\UpdateUserRequest;
9
use App\User;
10
use Illuminate\Http\JsonResponse;
11
use Illuminate\Http\Request;
12
use Password;
13
use Response;
14
15
/**
16
 * Class UsersManagementController
17
 *
18
 * @package Acacha\Users\Http\Controllers
19
 */
20
class UsersManagementController extends Controller
21
{
22
23
    /**
24
     * Show the application dashboard.
25
     *
26
     * @return Response
27
     */
28
    public function users()
29
    {
30
        $this->authorize('see-manage-users-view');
31
        return view('acacha_users::management');
32
    }
33
34
    /**
35
     * Display a listing of the resource.
36
     *
37
     * @return \Illuminate\Http\Response
38
     */
39
    public function index()
40
    {
41
        $this->authorize('list-users');
42
        return User::paginate();
43
    }
44
45
    /**
46
     * Store a newly created resource in storage.
47
     *
48
     * @param CreateUserRequest $request
49
     * @return \Illuminate\Http\JsonResponse
50
     */
51
    public function store(CreateUserRequest $request)
52
    {
53
        $user = User::create([
54
            'name' => $request->input('name'),
55
            'email' => $request->input('email'),
56
            'password' => bcrypt($request->input('password')),
0 ignored issues
show
Bug introduced by
It seems like $request->input('password') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, bcrypt() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
57
        ]);
58
59
        event(new UserCreated($user));
60
61
        return Response::json(['created' => true ]);
62
    }
63
64
    /**
65
     * Massive destroy.
66
     *
67
     * @param MassiveDestroyRequest $request
68
     * @return \Illuminate\Http\JsonResponse
69
     */
70
    public function massiveDestroy(MassiveDestroyRequest $request)
71
    {
72
        $this->authorize('massive-delete-users');
73
        return $this->executeDestroy($request->input('ids'));
74
    }
75
76
    /**
77
     * Execute destroy.
78
     *
79
     * @param $ids
80
     * @return \Illuminate\Http\JsonResponse
81
     */
82
    private function executeDestroy($ids){
83
        User::destroy($ids);
84
85
        //TODO
86
        // NOTE : this method trigger method "created" in UserObserver. Fire also and event to enable hooking.
87
//        event(new UserRemoved($user));
88
89
        return Response::json(['deleted' => true ]);
90
    }
91
92
    /**
93
     * Remove the specified resource from storage.
94
     *
95
     * @param  int|array  $id
96
     * @return \Illuminate\Http\JsonResponse
97
     */
98
    public function destroy($id)
99
    {
100
        $this->authorize('delete-users');
101
102
        return $this->executeDestroy($id);
103
    }
104
105
    /**
106
     * Display the specified resource.
107
     *
108
     * @param  int  $id
109
     * @return \Illuminate\Http\Response
110
     */
111
    public function show($id)
112
    {
113
        $this->authorize('view-users');
114
        return User::find($id);
115
    }
116
117
    /**
118
     * Update the specified resource in storage.
119
     *
120
     * @param UpdateUserRequest $request
121
     * @param $id
122
     * @return \Illuminate\Http\JsonResponse
123
     */
124
    public function update(UpdateUserRequest $request, $id)
125
    {
126
        $user = User::find($id);
127
        $user->update($request->intersect(['email','name','password']));
128
        return Response::json(['updated' => true ]);
129
    }
130
131
    /**
132
     * Register user by email
133
     *
134
     * @param Request $request
135
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
136
     */
137
    public function registerByEmail(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
138
    {
139
        $data = [];
140
        return view('acacha_users::register-by-email',$data);
141
    }
142
143
    /**
144
     * Send a reset link to the given user.
145
     *
146
     * @param Request $request
147
     * @return JsonResponse
148
     */
149
    public function sendResetLinkEmail(Request $request)
150
    {
151
        $this->validate($request, ['email' => 'required|email']);
152
153
        $response = Password::broker()->sendResetLink(
154
            $request->only('email')
155
        );
156
157
        if (Password::RESET_LINK_SENT) {
158
            return new JsonResponse(['status' => trans($response) ], 200);
159
        }
160
161
        return new JsonResponse(['email' => trans($response) ], 422);
162
163
    }
164
165
    /**
166
     * Send a reset link to the given users.
167
     *
168
     * @param Request $request
169
     * @return JsonResponse
170
     */
171
    public function massiveSendResetLinkEmail(Request $request)
172
    {
173
        $this->validate($request, ['ids' => 'required']);
174
175
        $errors = [];
176
        foreach ($request->input('ids') as $id) {
0 ignored issues
show
Bug introduced by
The expression $request->input('ids') of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
177
            $user = User::find($id);
178
            $response = Password::broker()->sendResetLink([ 'email' => $user->email ]);
179
            if (! Password::RESET_LINK_SENT) {
180
                dd('ERROR!');
181
                $errors[] = $response;
182
            }
183
        }
184
185
        if ( count($errors) > 0 ) return new JsonResponse(['status' => 'Error', 'errors' => $errors ], 422);
186
187
        return new JsonResponse(['status' => 'Done' ], 200);
188
    }
189
190
}