Issues (2037)

src/Chamilo/UserBundle/Security/Encoder.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\UserBundle\Security;
5
6
use Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder;
7
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
8
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
9
use Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder;
10
11
/**
12
 * Class Encoder.
13
 *
14
 * @package Chamilo\UserBundle\Security
15
 */
16
class Encoder implements PasswordEncoderInterface
17
{
18
    protected $method;
19
    protected $defaultEncoder;
20
21
    /**
22
     * @param $method
23
     */
24
    public function __construct($method)
25
    {
26
        $this->method = $method;
27
        switch ($this->method) {
28
            case 'none':
29
                $defaultEncoder = new PlaintextPasswordEncoder();
30
                break;
31
            case 'bcrypt':
32
                $defaultEncoder = new BCryptPasswordEncoder(4);
33
                break;
34
            case 'sha1':
35
            case 'md5':
36
                $defaultEncoder = new MessageDigestPasswordEncoder($this->method, false, 1);
37
                break;
38
        }
39
        $this->defaultEncoder = $defaultEncoder;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $defaultEncoder does not seem to be defined for all execution paths leading up to this point.
Loading history...
40
    }
41
42
    /**
43
     * @param string $raw
44
     * @param string $salt
45
     *
46
     * @return string
47
     */
48
    public function encodePassword($raw, $salt)
49
    {
50
        if ($this->method === 'bcrypt') {
51
            $salt = null;
52
        }
53
54
        return $this->defaultEncoder->encodePassword($raw, $salt);
55
    }
56
57
    /**
58
     * @param string $encoded
59
     * @param string $raw
60
     * @param string $salt
61
     *
62
     * @return bool
63
     */
64
    public function isPasswordValid($encoded, $raw, $salt)
65
    {
66
        if ($this->method === 'bcrypt') {
67
            $salt = null;
68
        }
69
70
        return $this->defaultEncoder->isPasswordValid($encoded, $raw, $salt);
71
    }
72
}
73