Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AdminController.php « Controllers « Http « app - github.com/cydrobolt/polr.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b3f90c3ebe66f5cc6d2e68638bfd549e72d3aa38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Hash;

use App\Models\Link;
use App\Models\User;
use App\Helpers\UserHelper;

class AdminController extends Controller {
    /**
     * Show the admin panel, and process admin AJAX requests.
     *
     * @return Response
     */
    public function displayAdminPage(Request $request) {
        if (!$this->isLoggedIn()) {
            return view('errors.404');
        }

        $username = session('username');
        $role = session('role');

        $admin_users = null;
        $admin_links = null;

        if ($this->currIsAdmin()) {
            $admin_users = User::paginate(15);
            $admin_links = Link::paginate(15);
        }

        $user_links = Link::where('creator', $username)
            ->paginate(15);

        return view('admin', [
            'role' => $role,
            'admin_users' => $admin_users,
            'admin_links' => $admin_links,
            'user_links' => $user_links
        ]);
    }

    public function changePassword(Request $request) {
        if (!$this->isLoggedIn()) {
            return view('errors.404');
        }
        $username = session('username');
        $old_password = $request->input('current_password');
        $new_password = $request->input('new_password');

        if (UserHelper::checkCredentials($username, $old_password) == false) {
            // Invalid credentials
            return redirect('admin')->with('error', 'Current password invalid. Try again.');
        }
        else {
            // Credentials are correct
            $user = UserHelper::getUserByUsername($username);
            $user->password = Hash::make($new_password);
            $user->save();

            $request->session()->flash('success', "Password changed successfully.");
            return redirect()->route('admin');
        }
    }
}