RedBeanPHP.php 2.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php
/**
 * gredu_labs.
 * 
 * @link https://github.com/eellak/gredu_labs for the canonical source repository
 *
 * @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
 * @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
 */

namespace GrEduLabs\Authentication\Adapter;

use RedBeanPHP\R;
use Zend\Authentication\Adapter\AbstractAdapter;
use Zend\Authentication\Result;
use Zend\Crypt\Password\PasswordInterface;

class RedBeanPHP extends AbstractAdapter
{
    /**
     * @var string
     */

Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
24
    private static $failMessage = 'Δεν ήταν δυνατή η σύνδεση. Παρακαλώ ελέγξτε το email και το συνθηματικό σας και δοκιμάστε ξανά.';
25 26 27 28 29 30
    /**
     * @var callable
     */
    protected $events;

    /**
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
31
     * @var callable
32
     */
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
33
    protected $resolveIdentityClass;
34 35 36 37 38 39

    /**
     * @var PasswordInterface
     */
    protected $crypt;

Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
40
    public function __construct(callable $events, callable $resolveIdentityClass, PasswordInterface $crypt)
41
    {
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
42 43 44
        $this->events               = $events;
        $this->resolveIdentityClass = $resolveIdentityClass;
        $this->crypt                = $crypt;
45 46 47 48 49 50 51
    }

    public function authenticate()
    {
        $events = $this->events;
        $events('trigger', 'authenticate', $this);

52 53 54 55

        $email             = filter_var($this->getIdentity(), FILTER_VALIDATE_EMAIL);
        $isValidCredential = filter_var(strlen(trim($this->getCredential())), FILTER_VALIDATE_INT, [
            'options'=> ['min_range' => 8],
56 57
        ]);

58 59 60 61 62 63
        if (!$email || !$isValidCredential) {
            return new Result(Result::FAILURE_CREDENTIAL_INVALID, null, [self::$failMessage]);
        }

        $user = R::findOne('user', 'mail = ? AND authentication_source = ?', [$email, 'DB']);

64 65 66 67 68 69 70 71
        if (!$user) {
            return new Result(Result::FAILURE_IDENTITY_NOT_FOUND, null, [self::$failMessage]);
        }

        if (!$this->crypt->verify($this->getCredential(), $user->password)) {
            return new Result(Result::FAILURE_CREDENTIAL_INVALID, null, [self::$failMessage]);
        }

Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
72
        $identityClass = call_user_func($this->resolveIdentityClass);
73
        $identity      = new $identityClass(
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
74
            $user->id,
75 76 77 78 79 80 81 82 83 84 85 86
            $user->uid,
            $user->mail,
            $user->displayName,
            $user->officeName,
            'DB'
        );

        $events('trigger', 'authenticate.success', $identity);

        return new Result(Result::SUCCESS, $identity, ['Authentication success']);
    }
}