summaryrefslogtreecommitdiff
path: root/app/Core/Ldap/Group.php
blob: e1f60ab5aa54cd2665da3257bc98477676d4acf6 (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
<?php

namespace Kanboard\Core\Ldap;

use LogicException;
use Kanboard\Group\LdapGroupProvider;

/**
 * LDAP Group Finder
 *
 * @package ldap
 * @author  Frederic Guillot
 */
class Group
{
    /**
     * Query
     *
     * @access protected
     * @var Query
     */
    protected $query;

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

    /**
     * Get groups
     *
     * @static
     * @access public
     * @param  Client    $client
     * @param  string    $query
     * @return LdapGroupProvider[]
     */
    public static function getGroups(Client $client, $query)
    {
        $self = new static(new Query($client));
        return $self->find($query);
    }

    /**
     * Find groups
     *
     * @access public
     * @param  string    $query
     * @return array
     */
    public function find($query)
    {
        $this->query->execute($this->getBasDn(), $query, $this->getAttributes());
        $groups = array();

        if ($this->query->hasResult()) {
            $groups = $this->build();
        }

        return $groups;
    }

    /**
     * Build groups list
     *
     * @access protected
     * @return array
     */
    protected function build()
    {
        $groups = array();

        foreach ($this->query->getEntries()->getAll() as $entry) {
            $groups[] = new LdapGroupProvider($entry->getDn(), $entry->getFirstValue($this->getAttributeName()));
        }

        return $groups;
    }

    /**
     * Ge the list of attributes to fetch when reading the LDAP group 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->getAttributeName(),
        )));
    }

    /**
     * Get LDAP group name attribute
     *
     * @access public
     * @return string
     */
    public function getAttributeName()
    {
        if (! LDAP_GROUP_ATTRIBUTE_NAME) {
            throw new LogicException('LDAP full name attribute empty, check the parameter LDAP_GROUP_ATTRIBUTE_NAME');
        }

        return strtolower(LDAP_GROUP_ATTRIBUTE_NAME);
    }

    /**
     * Get LDAP group base DN
     *
     * @access public
     * @return string
     */
    public function getBasDn()
    {
        if (! LDAP_GROUP_BASE_DN) {
            throw new LogicException('LDAP group base DN empty, check the parameter LDAP_GROUP_BASE_DN');
        }

        return LDAP_GROUP_BASE_DN;
    }
}