Issues (3)

src/View/Helper/IdentityHelper.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5
 * Licensed under The MIT License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
10
 * @link          https://cakephp.org CakePHP(tm) Project
11
 * @since         1.0.0
12
 * @license       https://opensource.org/licenses/mit-license.php MIT License
13
 */
14
namespace Phauthentic\Authentication\View\Helper;
15
16
use Phauthentic\Authentication\Identity\IdentityInterface;
17
use Cake\Utility\Hash;
18
use Cake\View\Helper;
19
use RuntimeException;
20
21
/**
22
 * Identity Helper
23
 *
24
 * A convenience helper to access the identity data
25
 */
26
class IdentityHelper extends Helper
27
{
28
    /**
29
     * Identity Object
30
     *
31
     * @var null|\Authentication\Identity\IdentityInterface
0 ignored issues
show
The type Authentication\Identity\IdentityInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
     */
33
    protected $_identity;
34
35
    /**
36
     * Constructor hook method.
37
     *
38
     * Implement this method to avoid having to overwrite the constructor and call parent.
39
     *
40
     * @param array $config The configuration settings provided to this helper.
41
     * @return void
42
     */
43 3
    public function initialize(array $config)
44
    {
45 3
        $this->_identity = $this->getView()->request->getAttribute('identity');
46
47 3
        if (empty($this->_identity)) {
48 1
            return;
49
        }
50
51 2
        if (!$this->_identity instanceof IdentityInterface) {
52 1
            throw new RuntimeException(sprintf('Identity found in request does not implement %s', IdentityInterface::class));
53
        }
54 1
    }
55
56
    /**
57
     * Gets the id of the current logged in identity
58
     *
59
     * @return int|null|string
60
     */
61 2
    public function getId()
62
    {
63 2
        if ($this->_identity === null) {
64 1
            return null;
65
        }
66
67 1
        return $this->_identity->getIdentifier();
68
    }
69
70
    /**
71
     * Checks if a user is logged in
72
     *
73
     * @return bool
74
     */
75 2
    public function isLoggedIn()
76
    {
77 2
        return $this->_identity !== null;
78
    }
79
80
    /**
81
     * This check can be used to tell if a record that belongs to some user is
82
     * the current logged in user and compare other fields as well
83
     *
84
     * If you have more complex requirements on visibility checks based on some
85
     * kind of permission you should use the Authorization plugin instead:
86
     *
87
     * https://github.com/cakephp/authorization
88
     *
89
     * This method is mostly a convenience method for simple cases and not
90
     * intended to replace any kind of proper authorization implementation.
91
     *
92
     * @param int|string $id Identity id to check against
93
     * @param string $field Name of the field in the identity data to check against, id by default
94
     * @return bool
95
     */
96 2
    public function is($id, $field = 'id')
97
    {
98 2
        return ($id === $this->get($field));
99
    }
100
101
    /**
102
     * Gets user data
103
     *
104
     * @param string|null $key Key of something you want to get from the identity data
105
     * @return mixed
106
     */
107 2
    public function get($key = null)
108
    {
109 2
        if (empty($this->_identity)) {
110 1
            return null;
111
        }
112
113 1
        if ($key === null) {
114 1
            return $this->_identity->getOriginalData();
115
        }
116
117 1
        return Hash::get($this->_identity, $key);
118
    }
119
}
120