summaryrefslogtreecommitdiffstats
path: root/src/Psecio/Gatekeeper/Provider/Laravel.php
blob: ca22d8d08d3a2510b1fdf4b62b9b2be0d0477840 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php

namespace Psecio\Gatekeeper\Provider;

use Illuminate\Auth\GenericUser;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\UserProviderInterface;
use Psecio\Gatekeeper\Gatekeeper;

/**
 * This provider is for only Laravel 4.x based applications
 */
class Laravel implements UserProviderInterface
{
    /**
     * Init the object and use the Laravel DB config to
     *     set up the PDO object
     */
    public function __construct()
    {
        $config = \Config::get('database.connections.gatekeeper');
        Gatekeeper::init(null, array(
            'type' => $config['driver'],
            'username' => $config['username'],
            'password' => $config['password'],
            'host' => $config['host'],
            'name' => 'gatekeeper'
        ));
    }

    /**
     * Retrieve a user by their unique identifier.
     *
     * @param integer $identifier User ID
     * @return \Illuminate\Auth\UserInterface
     */
    public function retrieveByID($identifier)
    {
        $user = Gatekeeper::modelFactory('UserModel');
        $user->findById($identifier);
        return $this->returnUser($user);
    }

    /**
     * Retrieve a user by the given credentials.
     *
     * @param  array $credentials
     * @return \Illuminate\Auth\UserInterface
     */
    public function retrieveByCredentials(array $credentials)
    {
        $user = Gatekeeper::modelFactory('UserModel');
        $result = $user->findByUsername($credentials['username']);
        return $this->returnUser($user);
    }

    /**
     * Validate a user against the given credentials.
     *
     * @param  \Illuminate\Auth\UserInterface  $user
     * @param  array $credentials
     * @return bool
     */
    public function validateCredentials(\Illuminate\Auth\UserInterface $user, array $credentials)
    {
        return Gatekeeper::authenticate($credentials);
    }

    /**
     * Get the current "remember me" token value
     *
     * @param string $identifier Identifier for token location
     * @param string $token      [description]
     * @throws \Exception Not implemented
     */
    public function retrieveByToken($identifier, $token)
    {
        return new \Exception('not implemented');
    }

    /**
     * Update the "remember me" token (not supported...yet)
     *
     * @param UserInterface $user User object
     * @param string $token Token string
     * @throws \Exception Not implemented
     */
    public function updateRememberToken(UserInterface $user, $token)
    {
        return new \Exception('not implemented');
    }

    /**
     * Reformat the Gatekeeper user into the Laravel user format
     *
     * @param \Psecio\Gatekeeper\UserModel $user User model instance
     * @return GenericUser instance
     */
    protected function returnUser(\Psecio\Gatekeeper\UserModel $user)
    {
        $attrs = array(
            'id' => $user->id,
            'username' => $user->username,
            'password' => $user->password,
            'name' => $user->firstName.' '.$user->lastName,
            'email' => $user->email
        );
        return new GenericUser($attrs);
    }
}