LibrariesController::postStoreMyAccount()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 25
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 36
rs 9.52
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Library;
6
use App\LibraryIp;
7
use App\Rules\TemporaryBarcodeExists;
8
use Carbon\Carbon;
9
use Illuminate\Http\Request;
10
use Illuminate\Http\Response;
11
use Illuminate\Support\Facades\Auth;
12
use Illuminate\Support\Facades\Session;
13
use Illuminate\Validation\ValidationException;
14
use Scriptotek\Alma\Client as AlmaClient;
15
16
class LibrariesController extends Controller
17
{
18
19
    /**
20
     * Validation error messages.
21
     *
22
     * @static array
23
     */
24
    protected $messages = [
25
        'name.required' => 'Norsk navn må fylles ut',
26
        'name.unique' => 'Norsk navn må være unikt',
27
        'name_eng.required' => 'Engelsk navn må fylles ut',
28
        'name_eng.unique' => 'Engelsk navn må være unikt',
29
        'email.required' => 'E-post må fylles ut',
30
        'email.unique' => 'E-post må være unik',
31
        'email.email' => 'E-post må være en gyldig epostadresse',
32
        'guest_ltid.regex' => 'LTID må være et gyldig LTID',
33
        'ip.required' => 'Adressen er tom',
34
        'ip.unique' => 'Adressen må være unik',
35
        'ip.ip' => 'Ugyldig ip-adresse',
36
    ];
37
38
    protected $lib;
39
40
    /**
41
     * Display a listing of the resource.
42
     *
43
     * @return Response
44
     */
45
    public function getIndex()
46
    {
47
        $items = Library::with('ips')->get();
48
        return response()->view('libraries.index', array(
49
            'libraries' => $items
50
        ));
51
    }
52
53
    /**
54
     * Display a form to create the resource.
55
     *
56
     * @return Response
57
     */
58
    public function getCreate()
59
    {
60
        return response()->view('libraries.create', array());
61
    }
62
63
    /**
64
     * Sets a new password. Note that it does *not store the model*.
65
     *
66
     * @param  string    $password
67
     * @param  string    $passwordRepeated
68
     * @return bool
69
     */
70
    protected function validateAndHashPassword($password, $passwordRepeated)
71
    {
72
        if (mb_strlen($password) < 8) {
73
            throw ValidationException::withMessages([
74
                'password' => ['Passordet er for kort (kortere enn 8 tegn).'],
75
            ]);
76
        }
77
78
        if ($password != $passwordRepeated) {
79
            throw ValidationException::withMessages([
80
                'password' => ['Du gjentok ikke passordet likt.'],
81
            ]);
82
        }
83
84
        return \Hash::make($password);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Hash::make($password) returns the type string which is incompatible with the documented return type boolean.
Loading history...
85
    }
86
87
    /**
88
     * Store a newly created resource in storage.
89
     *
90
     * @param Request $request
91
     * @param AlmaClient $alma
92
     * @return Response
93
     */
94
    public function postStore(Request $request, AlmaClient $alma)
95
    {
96
        $temporaryBarcode = new TemporaryBarcodeExists($alma);
97
98
        $rules = array(
99
            'name' => 'required|unique:libraries,name',
100
            'name_eng' => 'required|unique:libraries,name_eng',
101
            'email' => 'required|email|unique:libraries,email',
102
            'library_code' => 'sometimes|nullable|unique:libraries,library_code',
103
            'temporary_barcode' => [$temporaryBarcode],
104
        );
105
        \Validator::make($request->all(), $rules, $this->messages)->validate();
106
107
        $lib = new Library();
108
        $lib->password = $this->validateAndHashPassword($request->input('password'), $request->input('password2'));
109
        $lib->name = $request->input('name');
110
        $lib->name_eng = $request->input('name_eng');
111
        $lib->email = $request->input('email');
112
        $lib->library_code = $request->input('library_code');
113
        $lib->temporary_barcode = $temporaryBarcode->getNormalizedValue();
114
115
        if (!$lib->save()) {
116
            return redirect()->back()
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...b->errors)->withInput() returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
117
                ->withErrors($lib->errors)
0 ignored issues
show
Bug introduced by
The property errors does not seem to exist on App\Library. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
118
                ->withInput();
119
        }
120
121
        return redirect()->action('LibrariesController@getIndex')
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->actio...oteket ble opprettet!') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
122
            ->with('status', 'Biblioteket ble opprettet!');
123
    }
124
125
    /**
126
     * Display the specified resource.
127
     *
128
     * @param Library $library
129
     * @return Response
130
     */
131
    public function getShow(Library $library)
132
    {
133
        return response()->view('libraries.show', [
134
            'library' => $library
135
        ]);
136
    }
137
138
    public function getLogin()
139
    {
140
        return response()->view('login');
141
    }
142
143
    public function ipBasedLogin()
144
    {
145
        if (empty($_SERVER['REMOTE_ADDR'])) {
146
            return response('', 401)->header('Content-Type', 'text/plain');
147
        }
148
149
        $libraryIp = LibraryIp::where('ip', '=', $_SERVER['REMOTE_ADDR'])->first();
150
        if ($libraryIp) {
151
            Auth::login($libraryIp->library);
152
            Session::put('iplogin', true);
153
154
            $libraryIp->last_used = Carbon::now();
155
            $libraryIp->save();
156
157
            return response('', 204)->header('Content-Type', 'text/plain');
158
        }
159
160
        return response('', 401)->header('Content-Type', 'text/plain');
161
    }
162
163
    /**
164
     * Handle an authentication attempt.
165
     *
166
     * @param Request $request
167
     *
168
     * @return \Illuminate\Http\RedirectResponse
169
     */
170
    public function postLogin(Request $request)
171
    {
172
        $credentials = $request->only('email', 'password');
173
174
        if (Auth::attempt($credentials, true)) {
175
            Session::put('iplogin', false);
176
177
            return redirect()->intended('/');
178
        } else {
179
            return back()
180
                ->withInput()
181
                ->with('loginfailed', true);
182
        }
183
    }
184
185
    public function getLogout()
186
    {
187
        Auth::logout();
188
        return redirect()->to('/');
189
    }
190
191
    public function getMyAccount()
192
    {
193
        return response()->view('libraries.my', array(
194
            'library' => Auth::user(),
195
        ));
196
    }
197
198
    public function postStoreMyAccount(Request $request, AlmaClient $alma)
199
    {
200
        $library = Auth::user();
201
202
        $temporaryBarcode = new TemporaryBarcodeExists($alma);
203
204
        $rules = array(
205
            'name' => 'required|unique:libraries,name,' . $library->id,
206
            'name_eng' => 'required|unique:libraries,name_eng,' . $library->id,
207
            'email' => 'required|email|unique:libraries,email,' . $library->id,
208
            'library_code' => 'sometimes|nullable|unique:libraries,library_code,' . $library->id,
209
            'temporary_barcode' => [$temporaryBarcode],
210
        );
211
        \Validator::make($request->all(), $rules, $this->messages)->validate();
212
213
        $library->name = $request->input('name');
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on App\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
214
        $library->email = $request->input('email');
215
        $library->guest_ltid = $request->input('guest_ltid');
0 ignored issues
show
Bug introduced by
The property guest_ltid does not seem to exist on App\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
216
        $library->email = $request->input('email');
217
        $library->library_code = $request->input('library_code');
0 ignored issues
show
Bug introduced by
The property library_code does not exist on App\User. Did you mean library_id?
Loading history...
218
        $library->temporary_barcode = $temporaryBarcode->getNormalizedValue();
0 ignored issues
show
Bug introduced by
The property temporary_barcode does not seem to exist on App\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
219
220
        if (!$library->save()) {
221
            return redirect()->back()
222
                ->withErrors($library->errors)
0 ignored issues
show
Bug introduced by
The property errors does not seem to exist on App\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
223
                ->withInput();
224
        }
225
226
        if ($request->input('password') != '') {
227
            $password = $request->input('password');
228
            return redirect()->action('LibrariesController@getPassword')
229
                ->with('password', $password);
230
        }
231
232
        return redirect()->action('LibrariesController@getShow', $library->id)
233
            ->with('status', 'Kontoinformasjonen ble lagret.');
234
    }
235
236
    public function getPassword()
237
    {
238
        $library = Auth::user();
239
        return response()->view('libraries.password', array(
240
            'library' => $library,
241
            'password' => Session::get('password'),
242
        ));
243
    }
244
245
    public function postPassword(Request $request)
246
    {
247
        $library = Auth::user();
248
        $library->password = $this->validateAndHashPassword($request->input('password'), $request->input('password1'));
0 ignored issues
show
Bug introduced by
The property password does not seem to exist on App\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
249
        $library->save();
250
251
        return redirect()->action('LibrariesController@getShow', $library->id)
252
            ->with('status', 'Nytt passord ble satt.');
253
    }
254
255
    /**
256
     * Display a listing of the ips.
257
     *
258
     * @return Response
259
     */
260
    public function getMyIps()
261
    {
262
        return response()->view('libraries.ips.index', [
263
            'library' => Auth::user(),
264
        ]);
265
    }
266
267
    /**
268
     * Store a newly created resource in storage.
269
     *
270
     * @param Request $request
271
     * @return Response
272
     */
273
    public function storeIp(Request $request)
274
    {
275
        \Validator::make($request->all(), [
276
            'ip' => ['required', 'ip', 'unique:library_ips,ip'],
277
        ])->validate();
278
279
        $newIp = $request->input('ip');
280
281
        LibraryIp::create([
282
            'library_id' => Auth::user()->id,
283
            'ip' => $newIp,
284
        ]);
285
286
        return redirect()->action('LibrariesController@getMyIps')
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->actio...$newIp.' ble lagt til') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
287
            ->with('status', "IP-adressen $newIp ble lagt til");
288
    }
289
290
    /**
291
     * Remove the specified resource from storage.
292
     *
293
     * @param LibraryIp $ip
294
     * @return Response
295
     */
296
    public function removeIp(LibraryIp $ip)
297
    {
298
        if ($ip->library_id !== Auth::user()->id) {
299
            return redirect()->action('LibrariesController@getMyIps')
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->actio...e til ditt bibliotek.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
300
                ->with('status', 'IP-adressen hører ikke til ditt bibliotek.');
301
        }
302
303
        $ip->delete();
304
305
        return redirect()->action('LibrariesController@getMyIps')
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->actio...-adressen ble fjernet') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
306
            ->with('status', 'IP-adressen ble fjernet');
307
    }
308
}
309