group_wcf.php 5.26 KB
Newer Older
Fritz Webering's avatar
Fritz Webering committed
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
/**
 * Copyright (c) 2013 Fritz Webering <fritz@webering.eu>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the LICENSE file.
 */

namespace OCA\user_wcf;


/**
 * This loads group memberships from a WCF database for all groups listed
Fjen Undso's avatar
Fjen Undso committed
14
 * in the $authorizedGroups parameter. The database configuration is imported
Fritz Webering's avatar
Fritz Webering committed
15
 * from the WCF configuration file of the WCF installation given in the
Fjen Undso's avatar
Fjen Undso committed
16
 * $wcfPath parameter.
Fritz Webering's avatar
Fritz Webering committed
17
18
 */

19
class Group_WCF implements \OCP\GroupInterface {
Fritz Webering's avatar
Fritz Webering committed
20
    protected $db;
21

22
23
24
    public function __construct() {
        require(\OC_App::getAppPath('user_wcf').'/config/config.php');
        if (!file_exists($wcfPath) || !is_dir($wcfPath)) throw new \Exception('Not a valid WCF path: "'.$wcfPath.'"');
Fritz Webering's avatar
Fritz Webering committed
25
        $this->db = lib\WCF_DB::getInstance($wcfPath);
26
        $this->db->setAuthorizedGroups($authorizedGroups);
Fritz Webering's avatar
Fritz Webering committed
27
    }
28

Fritz Webering's avatar
Fritz Webering committed
29
    /**
30
31
32
33
34
35
36
37
38
39
40
     * Check if backend implements actions
     * @param int $actions bitwise-or'ed actions
     * @return boolean
     * @since 4.5.0
     *
     * Returns the supported actions as int to be
     * compared with \OC_Group_Backend::CREATE_GROUP etc.
     */
    public function implementsActions($actions) {
        return (bool)(GROUP_DETAILS
                & $actions);
Fritz Webering's avatar
Fritz Webering committed
41
42
43
    }

    /**
44
     * is user in group?
Fritz Webering's avatar
Fritz Webering committed
45
46
47
     * @param string $uid uid of the user
     * @param string $gid gid of the group
     * @return bool
48
     * @since 4.5.0
Fritz Webering's avatar
Fritz Webering committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
     *
     * Checks whether the user is member of a group or not.
     */
    public function inGroup($uid, $gid) {
        $inGroup = FALSE;
        $result = $this->db->prepare('1', 'username=? AND groupName=?');

        if ($result !== FALSE and $result->execute(array($uid, $gid))) {
            if ($result->fetch() !== FALSE) {
                $inGroup = TRUE;
            }
            $result->closeCursor();
        }
        return $inGroup;
    }

    /**
66
     * Get all groups a user belongs to
Fritz Webering's avatar
Fritz Webering committed
67
     * @param string $uid Name of the user
68
69
     * @return array an array of group names
     * @since 4.5.0
Fritz Webering's avatar
Fritz Webering committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
     *
     * This function fetches all groups a user belongs to. It does not check
     * if the user exists at all.
     */
    public function getUserGroups($uid) {
        $groups = array();
        $result = $this->db->prepare('groupName', 'username=?');

        if ($result !== FALSE and $result->execute(array($uid))) {
            foreach ($result as $row) {
                $groups[] = $row['groupName'];
            }
            $result->closeCursor();
        }
        return $groups;
    }

    /**
88
     * get a list of all groups
Fritz Webering's avatar
Fritz Webering committed
89
90
91
     * @param string $search
     * @param int $limit
     * @param int $offset
92
93
     * @return array an array of group names
     * @since 4.5.0
Fritz Webering's avatar
Fritz Webering committed
94
95
96
     *
     * Returns a list with all groups
     */
97
    public function getGroups($search = '', $limit = -1, $offset = 0) {
Fritz Webering's avatar
Fritz Webering committed
98
99
100
101
102
103
104
105
106
107
        $groups = array();
        $params = array();
        $where = NULL;
        $append = 'ORDER BY groupName';

        if ($search !== '') {
            $search = (string) $search;
            $where = 'groupName LIKE ?';
            $params[] = '%'.$search.'%';
        }
108
109
        if (!($limit === -1 or is_null($limit))) {
            $append .= ' LIMIT '.intval($liumit);
Fritz Webering's avatar
Fritz Webering committed
110
        }
111
        if (!($offset === 0 or is_null($offset))) {
Fritz Webering's avatar
Fritz Webering committed
112
113
114
115
116
117
            $append .= ' OFFSET '.intval($offset);
        }

        $result = $this->db->prepare('groupName', $where, $append);

        if ($result !== FALSE and $result->execute($params)) {
118
119
120
            if ($result->rowCount() <= 0) {
                User_WCF::warn('No groups returned from database.');
            }
Fritz Webering's avatar
Fritz Webering committed
121
122
123
124
125
            foreach ($result as $row) {
                $groups[] = $row['groupName'];
            }
            $result->closeCursor();
        }
126
127
128
        else {
            User_WCF::warn('Error executing statement to get group list.');
        }
Fritz Webering's avatar
Fritz Webering committed
129
130
        return $groups;
    }
131

Fritz Webering's avatar
Fritz Webering committed
132
133
134
135
    /**
     * check if a group exists
     * @param string $gid
     * @return bool
136
     * @since 4.5.0
Fritz Webering's avatar
Fritz Webering committed
137
138
139
140
141
142
143
144
145
146
147
148
149
     */
    public function groupExists($gid) {
        $exists = FALSE;
        $result = $this->db->prepare('groupName', 'groupName=?');

        if ($result !== FALSE and $result->execute(array($gid))) {
            $exists = ($result->fetch() !== FALSE);
            $result->closeCursor();
        }
        return $exists;
    }

    /**
150
     * get a list of all users in a group
Fritz Webering's avatar
Fritz Webering committed
151
152
153
154
     * @param string $gid
     * @param string $search
     * @param int $limit
     * @param int $offset
155
156
     * @return array an array of user ids
     * @since 4.5.0
Fritz Webering's avatar
Fritz Webering committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
     */
    public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
        $users = array();
        $params = array($gid);
        $where = 'groupName=?';
        $append = 'ORDER BY username';

        if ($search !== '') {
            $where .= ' AND username LIKE ?';
            $params[] = $search;
        }
        if ($limit !== -1) {
            $append .= ' LIMIT '.intval($limit);
        }
        if ($offset !== 0) {
            $append .= ' OFFSET '.intval($offset);
        }

        $result = $this->db->prepare('username', $where, $append);
        if ($result !== FALSE and $result->execute($params)) {
            foreach ($result as $row) {
                $users[] = $row['username'];
            }
            $result->closeCursor();
        }

        return $users;
    }
}