summaryrefslogtreecommitdiff
path: root/app/Core/Ldap/User.php
blob: e44a4dda4fd41a6151399a7ab2bbc6128cc9c223 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php

namespace Kanboard\Core\Ldap;

/**
 * LDAP User
 *
 * @package ldap
 * @author  Frederic Guillot
 */
class User
{
    /**
     * Query
     *
     * @access private
     * @var Query
     */
    private $query;

    /**
     * Constructor
     *
     * @access public
     * @param  Query   $query
     */
    public function __construct(Query $query = null)
    {
        $this->query = $query ?: new Query;
    }

    /**
     * Get user profile
     *
     * @access public
     * @param  resource  $ldap
     * @param  string    $baseDn
     * @param  string    $query
     * @return array
     */
    public function getProfile($ldap, $baseDn, $query)
    {
        $this->query->execute($ldap, $baseDn, $query, $this->getAttributes());
        $profile = array();

        if ($this->query->hasResult()) {
            $profile = $this->prepareProfile();
        }

        return $profile;
    }

    /**
     * Build user profile
     *
     * @access private
     * @return boolean|array
     */
    private function prepareProfile()
    {
        return array(
            'ldap_id' => $this->query->getAttribute('dn', ''),
            'username' => $this->query->getAttributeValue($this->getAttributeUsername()),
            'name' => $this->query->getAttributeValue($this->getAttributeName()),
            'email' => $this->query->getAttributeValue($this->getAttributeEmail()),
            'is_admin' => (int) $this->isMemberOf($this->query->getAttribute($this->getAttributeGroup(), array()), $this->getGroupAdminDn()),
            'is_project_admin' => (int) $this->isMemberOf($this->query->getAttribute($this->getAttributeGroup(), array()), $this->getGroupProjectAdminDn()),
            'is_ldap_user' => 1,
        );
    }

    /**
     * Check group membership
     *
     * @access public
     * @param  array   $group_entries
     * @param  string  $group_dn
     * @return boolean
     */
    public function isMemberOf(array $group_entries, $group_dn)
    {
        if (! isset($group_entries['count']) || empty($group_dn)) {
            return false;
        }

        for ($i = 0; $i < $group_entries['count']; $i++) {
            if ($group_entries[$i] === $group_dn) {
                return true;
            }
        }

        return false;
    }

    /**
     * Ge the list of attributes to fetch when reading the LDAP user entry
     *
     * Must returns array with index that start at 0 otherwise ldap_search returns a warning "Array initialization wrong"
     *
     * @access public
     * @return array
     */
    public function getAttributes()
    {
        return array_values(array_filter(array(
            $this->getAttributeUsername(),
            $this->getAttributeName(),
            $this->getAttributeEmail(),
            $this->getAttributeGroup(),
        )));
    }

    /**
     * Get LDAP account id attribute
     *
     * @access public
     * @return string
     */
    public function getAttributeUsername()
    {
        return LDAP_ACCOUNT_ID;
    }

    /**
     * Get LDAP account email attribute
     *
     * @access public
     * @return string
     */
    public function getAttributeEmail()
    {
        return LDAP_ACCOUNT_EMAIL;
    }

    /**
     * Get LDAP account name attribute
     *
     * @access public
     * @return string
     */
    public function getAttributeName()
    {
        return LDAP_ACCOUNT_FULLNAME;
    }

    /**
     * Get LDAP account memberof attribute
     *
     * @access public
     * @return string
     */
    public function getAttributeGroup()
    {
        return LDAP_ACCOUNT_MEMBEROF;
    }

    /**
     * Get LDAP admin group DN
     *
     * @access public
     * @return string
     */
    public function getGroupAdminDn()
    {
        return LDAP_GROUP_ADMIN_DN;
    }

    /**
     * Get LDAP project admin group DN
     *
     * @access public
     * @return string
     */
    public function getGroupProjectAdminDn()
    {
        return LDAP_GROUP_PROJECT_ADMIN_DN;
    }
}