Completed
Pull Request — master (#9261)
by John
44:50 queued 25:21
created
lib/public/IUserManager.php 1 patch
Indentation   +133 added lines, -133 removed lines patch added patch discarded remove patch
@@ -41,137 +41,137 @@
 block discarded – undo
41 41
  * @since 8.0.0
42 42
  */
43 43
 interface IUserManager {
44
-		/**
45
-	 * register a user backend
46
-	 *
47
-	 * @param \OCP\UserInterface $backend
48
-	 * @since 8.0.0
49
-	 */
50
-	public function registerBackend($backend);
51
-
52
-	/**
53
-	 * Get the active backends
54
-	 * @return \OCP\UserInterface[]
55
-	 * @since 8.0.0
56
-	 */
57
-	public function getBackends();
58
-
59
-	/**
60
-	 * remove a user backend
61
-	 *
62
-	 * @param \OCP\UserInterface $backend
63
-	 * @since 8.0.0
64
-	 */
65
-	public function removeBackend($backend);
66
-
67
-	/**
68
-	 * remove all user backends
69
-	 * @since 8.0.0
70
-	 */
71
-	public function clearBackends() ;
72
-
73
-	/**
74
-	 * get a user by user id
75
-	 *
76
-	 * @param string $uid
77
-	 * @return \OCP\IUser|null Either the user or null if the specified user does not exist
78
-	 * @since 8.0.0
79
-	 */
80
-	public function get($uid);
81
-
82
-	/**
83
-	 * check if a user exists
84
-	 *
85
-	 * @param string $uid
86
-	 * @return bool
87
-	 * @since 8.0.0
88
-	 */
89
-	public function userExists($uid);
90
-
91
-	/**
92
-	 * Check if the password is valid for the user
93
-	 *
94
-	 * @param string $loginName
95
-	 * @param string $password
96
-	 * @return mixed the User object on success, false otherwise
97
-	 * @since 8.0.0
98
-	 */
99
-	public function checkPassword($loginName, $password);
100
-
101
-	/**
102
-	 * search by user id
103
-	 *
104
-	 * @param string $pattern
105
-	 * @param int $limit
106
-	 * @param int $offset
107
-	 * @return \OCP\IUser[]
108
-	 * @since 8.0.0
109
-	 */
110
-	public function search($pattern, $limit = null, $offset = null);
111
-
112
-	/**
113
-	 * search by displayName
114
-	 *
115
-	 * @param string $pattern
116
-	 * @param int $limit
117
-	 * @param int $offset
118
-	 * @return \OCP\IUser[]
119
-	 * @since 8.0.0
120
-	 */
121
-	public function searchDisplayName($pattern, $limit = null, $offset = null);
122
-
123
-	/**
124
-	 * @param string $uid
125
-	 * @param string $password
126
-	 * @throws \InvalidArgumentException
127
-	 * @return bool|\OCP\IUser the created user of false
128
-	 * @since 8.0.0
129
-	 */
130
-	public function createUser($uid, $password);
131
-
132
-	/**
133
-	 * @param string $uid
134
-	 * @param string $password
135
-	 * @param UserInterface $backend
136
-	 * @return IUser|null
137
-	 * @throws \InvalidArgumentException
138
-	 * @since 12.0.0
139
-	 */
140
-	public function createUserFromBackend($uid, $password, UserInterface $backend);
141
-
142
-	/**
143
-	 * returns how many users per backend exist (if supported by backend)
144
-	 *
145
-	 * @return array an array of backend class as key and count number as value
146
-	 * @since 8.0.0
147
-	 */
148
-	public function countUsers();
149
-
150
-	/**
151
-	 * @param \Closure $callback
152
-	 * @param string $search
153
-	 * @since 9.0.0
154
-	 */
155
-	public function callForAllUsers(\Closure $callback, $search = '');
156
-
157
-	/**
158
-	 * returns how many users have logged in once
159
-	 *
160
-	 * @return int
161
-	 * @since 11.0.0
162
-	 */
163
-	public function countSeenUsers();
164
-
165
-	/**
166
-	 * @param \Closure $callback
167
-	 * @since 11.0.0
168
-	 */
169
-	public function callForSeenUsers(\Closure $callback);
170
-
171
-	/**
172
-	 * @param string $email
173
-	 * @return IUser[]
174
-	 * @since 9.1.0
175
-	 */
176
-	public function getByEmail($email);
44
+        /**
45
+         * register a user backend
46
+         *
47
+         * @param \OCP\UserInterface $backend
48
+         * @since 8.0.0
49
+         */
50
+    public function registerBackend($backend);
51
+
52
+    /**
53
+     * Get the active backends
54
+     * @return \OCP\UserInterface[]
55
+     * @since 8.0.0
56
+     */
57
+    public function getBackends();
58
+
59
+    /**
60
+     * remove a user backend
61
+     *
62
+     * @param \OCP\UserInterface $backend
63
+     * @since 8.0.0
64
+     */
65
+    public function removeBackend($backend);
66
+
67
+    /**
68
+     * remove all user backends
69
+     * @since 8.0.0
70
+     */
71
+    public function clearBackends() ;
72
+
73
+    /**
74
+     * get a user by user id
75
+     *
76
+     * @param string $uid
77
+     * @return \OCP\IUser|null Either the user or null if the specified user does not exist
78
+     * @since 8.0.0
79
+     */
80
+    public function get($uid);
81
+
82
+    /**
83
+     * check if a user exists
84
+     *
85
+     * @param string $uid
86
+     * @return bool
87
+     * @since 8.0.0
88
+     */
89
+    public function userExists($uid);
90
+
91
+    /**
92
+     * Check if the password is valid for the user
93
+     *
94
+     * @param string $loginName
95
+     * @param string $password
96
+     * @return mixed the User object on success, false otherwise
97
+     * @since 8.0.0
98
+     */
99
+    public function checkPassword($loginName, $password);
100
+
101
+    /**
102
+     * search by user id
103
+     *
104
+     * @param string $pattern
105
+     * @param int $limit
106
+     * @param int $offset
107
+     * @return \OCP\IUser[]
108
+     * @since 8.0.0
109
+     */
110
+    public function search($pattern, $limit = null, $offset = null);
111
+
112
+    /**
113
+     * search by displayName
114
+     *
115
+     * @param string $pattern
116
+     * @param int $limit
117
+     * @param int $offset
118
+     * @return \OCP\IUser[]
119
+     * @since 8.0.0
120
+     */
121
+    public function searchDisplayName($pattern, $limit = null, $offset = null);
122
+
123
+    /**
124
+     * @param string $uid
125
+     * @param string $password
126
+     * @throws \InvalidArgumentException
127
+     * @return bool|\OCP\IUser the created user of false
128
+     * @since 8.0.0
129
+     */
130
+    public function createUser($uid, $password);
131
+
132
+    /**
133
+     * @param string $uid
134
+     * @param string $password
135
+     * @param UserInterface $backend
136
+     * @return IUser|null
137
+     * @throws \InvalidArgumentException
138
+     * @since 12.0.0
139
+     */
140
+    public function createUserFromBackend($uid, $password, UserInterface $backend);
141
+
142
+    /**
143
+     * returns how many users per backend exist (if supported by backend)
144
+     *
145
+     * @return array an array of backend class as key and count number as value
146
+     * @since 8.0.0
147
+     */
148
+    public function countUsers();
149
+
150
+    /**
151
+     * @param \Closure $callback
152
+     * @param string $search
153
+     * @since 9.0.0
154
+     */
155
+    public function callForAllUsers(\Closure $callback, $search = '');
156
+
157
+    /**
158
+     * returns how many users have logged in once
159
+     *
160
+     * @return int
161
+     * @since 11.0.0
162
+     */
163
+    public function countSeenUsers();
164
+
165
+    /**
166
+     * @param \Closure $callback
167
+     * @since 11.0.0
168
+     */
169
+    public function callForSeenUsers(\Closure $callback);
170
+
171
+    /**
172
+     * @param string $email
173
+     * @return IUser[]
174
+     * @since 9.1.0
175
+     */
176
+    public function getByEmail($email);
177 177
 }
Please login to merge, or discard this patch.
lib/private/Group/MetaData.php 2 patches
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -66,7 +66,7 @@  discard block
 block discarded – undo
66 66
 			IUserSession $userSession
67 67
 			) {
68 68
 		$this->user = $user;
69
-		$this->isAdmin = (bool)$isAdmin;
69
+		$this->isAdmin = (bool) $isAdmin;
70 70
 		$this->groupManager = $groupManager;
71 71
 		$this->userManager = $userManager;
72 72
 		$this->userSession = $userSession;
@@ -83,8 +83,8 @@  discard block
 block discarded – undo
83 83
 	 * @return array
84 84
 	 */
85 85
 	public function get($groupSearch = '', $userSearch = '') {
86
-		$key = $groupSearch . '::' . $userSearch;
87
-		if(isset($this->metaData[$key])) {
86
+		$key = $groupSearch.'::'.$userSearch;
87
+		if (isset($this->metaData[$key])) {
88 88
 			return $this->metaData[$key];
89 89
 		}
90 90
 
@@ -95,7 +95,7 @@  discard block
 block discarded – undo
95 95
 		$sortAdminGroupsIndex = 0;
96 96
 		$sortAdminGroupsKeys = array();
97 97
 
98
-		foreach($this->getGroups($groupSearch) as $group) {
98
+		foreach ($this->getGroups($groupSearch) as $group) {
99 99
 			$groupMetaData = $this->generateGroupMetaData($group, $userSearch);
100 100
 			if (strtolower($group->getGID()) !== 'admin') {
101 101
 				$this->addEntry(
@@ -193,11 +193,11 @@  discard block
 block discarded – undo
193 193
 	 * @return \OCP\IGroup[]
194 194
 	 */
195 195
 	protected function getGroups($search = '') {
196
-		if($this->isAdmin) {
196
+		if ($this->isAdmin) {
197 197
 			return $this->groupManager->search($search);
198 198
 		} else {
199 199
 			$userObject = $this->userSession->getUser();
200
-			if($userObject !== null) {
200
+			if ($userObject !== null) {
201 201
 				$groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject);
202 202
 			} else {
203 203
 				$groups = [];
Please login to merge, or discard this patch.
Indentation   +174 added lines, -174 removed lines patch added patch discarded remove patch
@@ -32,178 +32,178 @@
 block discarded – undo
32 32
 use OCP\IUserManager;
33 33
 
34 34
 class MetaData {
35
-	const SORT_NONE = 0;
36
-	const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends
37
-	const SORT_GROUPNAME = 2;
38
-
39
-	/** @var string */
40
-	protected $user;
41
-	/** @var bool */
42
-	protected $isAdmin;
43
-	/** @var array */
44
-	protected $metaData = array();
45
-	/** @var IGroupManager */
46
-	protected $groupManager;
47
-	/** @var IUserManager */
48
-	protected $userManager;
49
-	/** @var bool */
50
-	protected $sorting = false;
51
-	/** @var IUserSession */
52
-	protected $userSession;
53
-
54
-	/**
55
-	 * @param string $user the uid of the current user
56
-	 * @param bool $isAdmin whether the current users is an admin
57
-	 * @param IGroupManager $groupManager
58
-	 * @param IUserManager $userManager
59
-	 * @param IUserSession $userSession
60
-	 */
61
-	public function __construct(
62
-			$user,
63
-			$isAdmin,
64
-			IGroupManager $groupManager,
65
-			IUserManager $userManager,
66
-			IUserSession $userSession
67
-			) {
68
-		$this->user = $user;
69
-		$this->isAdmin = (bool)$isAdmin;
70
-		$this->groupManager = $groupManager;
71
-		$this->userManager = $userManager;
72
-		$this->userSession = $userSession;
73
-	}
74
-
75
-	/**
76
-	 * returns an array with meta data about all available groups
77
-	 * the array is structured as follows:
78
-	 * [0] array containing meta data about admin groups
79
-	 * [1] array containing meta data about unprivileged groups
80
-	 * @param string $groupSearch only effective when instance was created with
81
-	 * isAdmin being true
82
-	 * @param string $userSearch the pattern users are search for
83
-	 * @return array
84
-	 */
85
-	public function get($groupSearch = '', $userSearch = '') {
86
-		$key = $groupSearch . '::' . $userSearch;
87
-		if(isset($this->metaData[$key])) {
88
-			return $this->metaData[$key];
89
-		}
90
-
91
-		$adminGroups = array();
92
-		$groups = array();
93
-		$sortGroupsIndex = 0;
94
-		$sortGroupsKeys = array();
95
-		$sortAdminGroupsIndex = 0;
96
-		$sortAdminGroupsKeys = array();
97
-
98
-		foreach($this->getGroups($groupSearch) as $group) {
99
-			$groupMetaData = $this->generateGroupMetaData($group, $userSearch);
100
-			if (strtolower($group->getGID()) !== 'admin') {
101
-				$this->addEntry(
102
-					$groups,
103
-					$sortGroupsKeys,
104
-					$sortGroupsIndex,
105
-					$groupMetaData);
106
-			} else {
107
-				//admin group is hard coded to 'admin' for now. In future,
108
-				//backends may define admin groups too. Then the if statement
109
-				//has to be adjusted accordingly.
110
-				$this->addEntry(
111
-					$adminGroups,
112
-					$sortAdminGroupsKeys,
113
-					$sortAdminGroupsIndex,
114
-					$groupMetaData);
115
-			}
116
-		}
117
-
118
-		//whether sorting is necessary is will be checked in sort()
119
-		$this->sort($groups, $sortGroupsKeys);
120
-		$this->sort($adminGroups, $sortAdminGroupsKeys);
121
-
122
-		$this->metaData[$key] = array($adminGroups, $groups);
123
-		return $this->metaData[$key];
124
-	}
125
-
126
-	/**
127
-	 * sets the sort mode, see SORT_* constants for supported modes
128
-	 *
129
-	 * @param int $sortMode
130
-	 */
131
-	public function setSorting($sortMode) {
132
-		switch ($sortMode) {
133
-			case self::SORT_USERCOUNT:
134
-			case self::SORT_GROUPNAME:
135
-				$this->sorting = $sortMode;
136
-				break;
137
-
138
-			default:
139
-				$this->sorting = self::SORT_NONE;
140
-		}
141
-	}
142
-
143
-	/**
144
-	 * adds an group entry to the resulting array
145
-	 * @param array $entries the resulting array, by reference
146
-	 * @param array $sortKeys the sort key array, by reference
147
-	 * @param int $sortIndex the sort key index, by reference
148
-	 * @param array $data the group's meta data as returned by generateGroupMetaData()
149
-	 */
150
-	private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) {
151
-		$entries[] = $data;
152
-		if ($this->sorting === self::SORT_USERCOUNT) {
153
-			$sortKeys[$sortIndex] = $data['usercount'];
154
-			$sortIndex++;
155
-		} else if ($this->sorting === self::SORT_GROUPNAME) {
156
-			$sortKeys[$sortIndex] = $data['name'];
157
-			$sortIndex++;
158
-		}
159
-	}
160
-
161
-	/**
162
-	 * creates an array containing the group meta data
163
-	 * @param \OCP\IGroup $group
164
-	 * @param string $userSearch
165
-	 * @return array with the keys 'id', 'name', 'usercount' and 'disabled'
166
-	 */
167
-	private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) {
168
-		return array(
169
-				'id' => $group->getGID(),
170
-				'name' => $group->getDisplayName(),
171
-				'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0,
172
-				'disabled' => $group->countDisabled()
173
-			);
174
-	}
175
-
176
-	/**
177
-	 * sorts the result array, if applicable
178
-	 * @param array $entries the result array, by reference
179
-	 * @param array $sortKeys the array containing the sort keys
180
-	 * @param return null
181
-	 */
182
-	private function sort(&$entries, $sortKeys) {
183
-		if ($this->sorting === self::SORT_USERCOUNT) {
184
-			array_multisort($sortKeys, SORT_DESC, $entries);
185
-		} else if ($this->sorting === self::SORT_GROUPNAME) {
186
-			array_multisort($sortKeys, SORT_ASC, $entries);
187
-		}
188
-	}
189
-
190
-	/**
191
-	 * returns the available groups
192
-	 * @param string $search a search string
193
-	 * @return \OCP\IGroup[]
194
-	 */
195
-	protected function getGroups($search = '') {
196
-		if($this->isAdmin) {
197
-			return $this->groupManager->search($search);
198
-		} else {
199
-			$userObject = $this->userSession->getUser();
200
-			if($userObject !== null) {
201
-				$groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject);
202
-			} else {
203
-				$groups = [];
204
-			}
205
-
206
-			return $groups;
207
-		}
208
-	}
35
+    const SORT_NONE = 0;
36
+    const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends
37
+    const SORT_GROUPNAME = 2;
38
+
39
+    /** @var string */
40
+    protected $user;
41
+    /** @var bool */
42
+    protected $isAdmin;
43
+    /** @var array */
44
+    protected $metaData = array();
45
+    /** @var IGroupManager */
46
+    protected $groupManager;
47
+    /** @var IUserManager */
48
+    protected $userManager;
49
+    /** @var bool */
50
+    protected $sorting = false;
51
+    /** @var IUserSession */
52
+    protected $userSession;
53
+
54
+    /**
55
+     * @param string $user the uid of the current user
56
+     * @param bool $isAdmin whether the current users is an admin
57
+     * @param IGroupManager $groupManager
58
+     * @param IUserManager $userManager
59
+     * @param IUserSession $userSession
60
+     */
61
+    public function __construct(
62
+            $user,
63
+            $isAdmin,
64
+            IGroupManager $groupManager,
65
+            IUserManager $userManager,
66
+            IUserSession $userSession
67
+            ) {
68
+        $this->user = $user;
69
+        $this->isAdmin = (bool)$isAdmin;
70
+        $this->groupManager = $groupManager;
71
+        $this->userManager = $userManager;
72
+        $this->userSession = $userSession;
73
+    }
74
+
75
+    /**
76
+     * returns an array with meta data about all available groups
77
+     * the array is structured as follows:
78
+     * [0] array containing meta data about admin groups
79
+     * [1] array containing meta data about unprivileged groups
80
+     * @param string $groupSearch only effective when instance was created with
81
+     * isAdmin being true
82
+     * @param string $userSearch the pattern users are search for
83
+     * @return array
84
+     */
85
+    public function get($groupSearch = '', $userSearch = '') {
86
+        $key = $groupSearch . '::' . $userSearch;
87
+        if(isset($this->metaData[$key])) {
88
+            return $this->metaData[$key];
89
+        }
90
+
91
+        $adminGroups = array();
92
+        $groups = array();
93
+        $sortGroupsIndex = 0;
94
+        $sortGroupsKeys = array();
95
+        $sortAdminGroupsIndex = 0;
96
+        $sortAdminGroupsKeys = array();
97
+
98
+        foreach($this->getGroups($groupSearch) as $group) {
99
+            $groupMetaData = $this->generateGroupMetaData($group, $userSearch);
100
+            if (strtolower($group->getGID()) !== 'admin') {
101
+                $this->addEntry(
102
+                    $groups,
103
+                    $sortGroupsKeys,
104
+                    $sortGroupsIndex,
105
+                    $groupMetaData);
106
+            } else {
107
+                //admin group is hard coded to 'admin' for now. In future,
108
+                //backends may define admin groups too. Then the if statement
109
+                //has to be adjusted accordingly.
110
+                $this->addEntry(
111
+                    $adminGroups,
112
+                    $sortAdminGroupsKeys,
113
+                    $sortAdminGroupsIndex,
114
+                    $groupMetaData);
115
+            }
116
+        }
117
+
118
+        //whether sorting is necessary is will be checked in sort()
119
+        $this->sort($groups, $sortGroupsKeys);
120
+        $this->sort($adminGroups, $sortAdminGroupsKeys);
121
+
122
+        $this->metaData[$key] = array($adminGroups, $groups);
123
+        return $this->metaData[$key];
124
+    }
125
+
126
+    /**
127
+     * sets the sort mode, see SORT_* constants for supported modes
128
+     *
129
+     * @param int $sortMode
130
+     */
131
+    public function setSorting($sortMode) {
132
+        switch ($sortMode) {
133
+            case self::SORT_USERCOUNT:
134
+            case self::SORT_GROUPNAME:
135
+                $this->sorting = $sortMode;
136
+                break;
137
+
138
+            default:
139
+                $this->sorting = self::SORT_NONE;
140
+        }
141
+    }
142
+
143
+    /**
144
+     * adds an group entry to the resulting array
145
+     * @param array $entries the resulting array, by reference
146
+     * @param array $sortKeys the sort key array, by reference
147
+     * @param int $sortIndex the sort key index, by reference
148
+     * @param array $data the group's meta data as returned by generateGroupMetaData()
149
+     */
150
+    private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) {
151
+        $entries[] = $data;
152
+        if ($this->sorting === self::SORT_USERCOUNT) {
153
+            $sortKeys[$sortIndex] = $data['usercount'];
154
+            $sortIndex++;
155
+        } else if ($this->sorting === self::SORT_GROUPNAME) {
156
+            $sortKeys[$sortIndex] = $data['name'];
157
+            $sortIndex++;
158
+        }
159
+    }
160
+
161
+    /**
162
+     * creates an array containing the group meta data
163
+     * @param \OCP\IGroup $group
164
+     * @param string $userSearch
165
+     * @return array with the keys 'id', 'name', 'usercount' and 'disabled'
166
+     */
167
+    private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) {
168
+        return array(
169
+                'id' => $group->getGID(),
170
+                'name' => $group->getDisplayName(),
171
+                'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0,
172
+                'disabled' => $group->countDisabled()
173
+            );
174
+    }
175
+
176
+    /**
177
+     * sorts the result array, if applicable
178
+     * @param array $entries the result array, by reference
179
+     * @param array $sortKeys the array containing the sort keys
180
+     * @param return null
181
+     */
182
+    private function sort(&$entries, $sortKeys) {
183
+        if ($this->sorting === self::SORT_USERCOUNT) {
184
+            array_multisort($sortKeys, SORT_DESC, $entries);
185
+        } else if ($this->sorting === self::SORT_GROUPNAME) {
186
+            array_multisort($sortKeys, SORT_ASC, $entries);
187
+        }
188
+    }
189
+
190
+    /**
191
+     * returns the available groups
192
+     * @param string $search a search string
193
+     * @return \OCP\IGroup[]
194
+     */
195
+    protected function getGroups($search = '') {
196
+        if($this->isAdmin) {
197
+            return $this->groupManager->search($search);
198
+        } else {
199
+            $userObject = $this->userSession->getUser();
200
+            if($userObject !== null) {
201
+                $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject);
202
+            } else {
203
+                $groups = [];
204
+            }
205
+
206
+            return $groups;
207
+        }
208
+    }
209 209
 }
Please login to merge, or discard this patch.
settings/users.php 2 patches
Indentation   +42 added lines, -42 removed lines patch added patch discarded remove patch
@@ -48,18 +48,18 @@  discard block
 block discarded – undo
48 48
 $config = \OC::$server->getConfig();
49 49
 
50 50
 if ($config->getSystemValue('sort_groups_by_name', false)) {
51
-	$sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME;
51
+    $sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME;
52 52
 } else {
53
-	$isLDAPUsed = false;
54
-	if ($appManager->isEnabledForUser('user_ldap')) {
55
-		$isLDAPUsed =
56
-			$groupManager->isBackendUsed('\OCA\User_LDAP\Group_LDAP')
57
-			|| $groupManager->isBackendUsed('\OCA\User_LDAP\Group_Proxy');
58
-		if ($isLDAPUsed) {
59
-			// LDAP user count can be slow, so we sort by group name here
60
-			$sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME;
61
-		}
62
-	}
53
+    $isLDAPUsed = false;
54
+    if ($appManager->isEnabledForUser('user_ldap')) {
55
+        $isLDAPUsed =
56
+            $groupManager->isBackendUsed('\OCA\User_LDAP\Group_LDAP')
57
+            || $groupManager->isBackendUsed('\OCA\User_LDAP\Group_Proxy');
58
+        if ($isLDAPUsed) {
59
+            // LDAP user count can be slow, so we sort by group name here
60
+            $sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME;
61
+        }
62
+    }
63 63
 }
64 64
 
65 65
 $uid = \OC_User::getUser();
@@ -68,68 +68,68 @@  discard block
 block discarded – undo
68 68
 $isDisabled = true;
69 69
 $user = $userManager->get($uid);
70 70
 if ($user) {
71
-	$isDisabled = !$user->isEnabled();
71
+    $isDisabled = !$user->isEnabled();
72 72
 }
73 73
 
74 74
 $groupsInfo = new \OC\Group\MetaData(
75
-	$uid,
76
-	$isAdmin,
77
-	$groupManager,
78
-	$userManager,
79
-	\OC::$server->getUserSession()
75
+    $uid,
76
+    $isAdmin,
77
+    $groupManager,
78
+    $userManager,
79
+    \OC::$server->getUserSession()
80 80
 );
81 81
 
82 82
 $groupsInfo->setSorting($sortGroupsBy);
83 83
 list($adminGroup, $groups) = $groupsInfo->get();
84 84
 
85 85
 $recoveryAdminEnabled = $appManager->isEnabledForUser('encryption') &&
86
-					    $config->getAppValue( 'encryption', 'recoveryAdminEnabled', '0');
86
+                        $config->getAppValue( 'encryption', 'recoveryAdminEnabled', '0');
87 87
 
88 88
 if ($isAdmin) {
89
-	$subAdmins = \OC::$server->getGroupManager()->getSubAdmin()->getAllSubAdmins();
90
-	// New class returns IUser[] so convert back
91
-	$result = [];
92
-	foreach ($subAdmins as $subAdmin) {
93
-		$result[] = [
94
-			'gid' => $subAdmin['group']->getGID(),
95
-			'uid' => $subAdmin['user']->getUID(),
96
-		];
97
-	}
98
-	$subAdmins = $result;
89
+    $subAdmins = \OC::$server->getGroupManager()->getSubAdmin()->getAllSubAdmins();
90
+    // New class returns IUser[] so convert back
91
+    $result = [];
92
+    foreach ($subAdmins as $subAdmin) {
93
+        $result[] = [
94
+            'gid' => $subAdmin['group']->getGID(),
95
+            'uid' => $subAdmin['user']->getUID(),
96
+        ];
97
+    }
98
+    $subAdmins = $result;
99 99
 } else {
100
-	/* Retrieve group IDs from $groups array, so we can pass that information into OC_Group::displayNamesInGroups() */
101
-	$gids = array();
102
-	foreach($groups as $group) {
103
-		if (isset($group['id'])) {
104
-			$gids[] = $group['id'];
105
-		}
106
-	}
107
-	$subAdmins = false;
100
+    /* Retrieve group IDs from $groups array, so we can pass that information into OC_Group::displayNamesInGroups() */
101
+    $gids = array();
102
+    foreach($groups as $group) {
103
+        if (isset($group['id'])) {
104
+            $gids[] = $group['id'];
105
+        }
106
+    }
107
+    $subAdmins = false;
108 108
 }
109 109
 
110 110
 // remove disabled from total count
111 111
 foreach($groups as $key => $group) {
112
-	$groups[$key]['usercount'] -= $group['disabled'];
112
+    $groups[$key]['usercount'] -= $group['disabled'];
113 113
 }
114 114
 
115 115
 $disabledUsers = $isLDAPUsed ? 0 : $isAdmin ? $userManager->countDisabledUsers() : $userManager->countDisabledUsersOfGroups();
116 116
 $disabledUsersGroup = [
117
-	'id' => '_disabledUsers',
118
-	'name' => '_disabledUsers',
119
-	'usercount' => $disabledUsers
117
+    'id' => '_disabledUsers',
118
+    'name' => '_disabledUsers',
119
+    'usercount' => $disabledUsers
120 120
 ];
121 121
 
122 122
 // load preset quotas
123 123
 $quotaPreset=$config->getAppValue('files', 'quota_preset', '1 GB, 5 GB, 10 GB');
124 124
 $quotaPreset=explode(',', $quotaPreset);
125 125
 foreach($quotaPreset as &$preset) {
126
-	$preset=trim($preset);
126
+    $preset=trim($preset);
127 127
 }
128 128
 $quotaPreset=array_diff($quotaPreset, array('default', 'none'));
129 129
 
130 130
 $defaultQuota=$config->getAppValue('files', 'default_quota', 'none');
131 131
 $defaultQuotaIsUserDefined=array_search($defaultQuota, $quotaPreset)===false
132
-	&& array_search($defaultQuota, array('none', 'default'))===false;
132
+    && array_search($defaultQuota, array('none', 'default'))===false;
133 133
 
134 134
 \OC::$server->getEventDispatcher()->dispatch('OC\Settings\Users::loadAdditionalScripts');
135 135
 
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
 list($adminGroup, $groups) = $groupsInfo->get();
84 84
 
85 85
 $recoveryAdminEnabled = $appManager->isEnabledForUser('encryption') &&
86
-					    $config->getAppValue( 'encryption', 'recoveryAdminEnabled', '0');
86
+					    $config->getAppValue('encryption', 'recoveryAdminEnabled', '0');
87 87
 
88 88
 if ($isAdmin) {
89 89
 	$subAdmins = \OC::$server->getGroupManager()->getSubAdmin()->getAllSubAdmins();
@@ -99,7 +99,7 @@  discard block
 block discarded – undo
99 99
 } else {
100 100
 	/* Retrieve group IDs from $groups array, so we can pass that information into OC_Group::displayNamesInGroups() */
101 101
 	$gids = array();
102
-	foreach($groups as $group) {
102
+	foreach ($groups as $group) {
103 103
 		if (isset($group['id'])) {
104 104
 			$gids[] = $group['id'];
105 105
 		}
@@ -108,7 +108,7 @@  discard block
 block discarded – undo
108 108
 }
109 109
 
110 110
 // remove disabled from total count
111
-foreach($groups as $key => $group) {
111
+foreach ($groups as $key => $group) {
112 112
 	$groups[$key]['usercount'] -= $group['disabled'];
113 113
 }
114 114
 
@@ -120,16 +120,16 @@  discard block
 block discarded – undo
120 120
 ];
121 121
 
122 122
 // load preset quotas
123
-$quotaPreset=$config->getAppValue('files', 'quota_preset', '1 GB, 5 GB, 10 GB');
124
-$quotaPreset=explode(',', $quotaPreset);
125
-foreach($quotaPreset as &$preset) {
126
-	$preset=trim($preset);
123
+$quotaPreset = $config->getAppValue('files', 'quota_preset', '1 GB, 5 GB, 10 GB');
124
+$quotaPreset = explode(',', $quotaPreset);
125
+foreach ($quotaPreset as &$preset) {
126
+	$preset = trim($preset);
127 127
 }
128
-$quotaPreset=array_diff($quotaPreset, array('default', 'none'));
128
+$quotaPreset = array_diff($quotaPreset, array('default', 'none'));
129 129
 
130
-$defaultQuota=$config->getAppValue('files', 'default_quota', 'none');
131
-$defaultQuotaIsUserDefined=array_search($defaultQuota, $quotaPreset)===false
132
-	&& array_search($defaultQuota, array('none', 'default'))===false;
130
+$defaultQuota = $config->getAppValue('files', 'default_quota', 'none');
131
+$defaultQuotaIsUserDefined = array_search($defaultQuota, $quotaPreset) === false
132
+	&& array_search($defaultQuota, array('none', 'default')) === false;
133 133
 
134 134
 \OC::$server->getEventDispatcher()->dispatch('OC\Settings\Users::loadAdditionalScripts');
135 135
 
@@ -138,7 +138,7 @@  discard block
 block discarded – undo
138 138
 $tmpl->assign('sortGroups', $sortGroupsBy);
139 139
 $tmpl->assign('adminGroup', $adminGroup);
140 140
 $tmpl->assign('disabledUsersGroup', $disabledUsersGroup);
141
-$tmpl->assign('isAdmin', (int)$isAdmin);
141
+$tmpl->assign('isAdmin', (int) $isAdmin);
142 142
 $tmpl->assign('subadmins', $subAdmins);
143 143
 $tmpl->assign('numofgroups', count($groups) + count($adminGroup));
144 144
 $tmpl->assign('quota_preset', $quotaPreset);
Please login to merge, or discard this patch.
settings/Controller/GroupsController.php 1 patch
Indentation   +113 added lines, -113 removed lines patch added patch discarded remove patch
@@ -39,125 +39,125 @@
 block discarded – undo
39 39
  * @package OC\Settings\Controller
40 40
  */
41 41
 class GroupsController extends Controller {
42
-	/** @var IGroupManager */
43
-	private $groupManager;
44
-	/** @var IUserManager */
45
-	private $userManager;
46
-	/** @var IL10N */
47
-	private $l10n;
48
-	/** @var IUserSession */
49
-	private $userSession;
50
-	/** @var bool */
51
-	private $isAdmin;
42
+    /** @var IGroupManager */
43
+    private $groupManager;
44
+    /** @var IUserManager */
45
+    private $userManager;
46
+    /** @var IL10N */
47
+    private $l10n;
48
+    /** @var IUserSession */
49
+    private $userSession;
50
+    /** @var bool */
51
+    private $isAdmin;
52 52
 
53
-	/**
54
-	 * @param string $appName
55
-	 * @param IRequest $request
56
-	 * @param IGroupManager $groupManager
57
-	 * @param IUserSession $userSession
58
-	 * @param bool $isAdmin
59
-	 * @param IL10N $l10n
60
-	 */
61
-	public function __construct($appName,
62
-								IRequest $request,
63
-								IGroupManager $groupManager,
64
-								IUserManager $userManager,
65
-								IUserSession $userSession,
66
-								$isAdmin,
67
-								IL10N $l10n) {
68
-		parent::__construct($appName, $request);
69
-		$this->groupManager = $groupManager;
70
-		$this->userManager = $userManager;
71
-		$this->userSession = $userSession;
72
-		$this->isAdmin = $isAdmin;
73
-		$this->l10n = $l10n;
74
-	}
53
+    /**
54
+     * @param string $appName
55
+     * @param IRequest $request
56
+     * @param IGroupManager $groupManager
57
+     * @param IUserSession $userSession
58
+     * @param bool $isAdmin
59
+     * @param IL10N $l10n
60
+     */
61
+    public function __construct($appName,
62
+                                IRequest $request,
63
+                                IGroupManager $groupManager,
64
+                                IUserManager $userManager,
65
+                                IUserSession $userSession,
66
+                                $isAdmin,
67
+                                IL10N $l10n) {
68
+        parent::__construct($appName, $request);
69
+        $this->groupManager = $groupManager;
70
+        $this->userManager = $userManager;
71
+        $this->userSession = $userSession;
72
+        $this->isAdmin = $isAdmin;
73
+        $this->l10n = $l10n;
74
+    }
75 75
 
76
-	/**
77
-	 * @NoAdminRequired
78
-	 *
79
-	 * @param string $pattern
80
-	 * @param bool $filterGroups
81
-	 * @param int $sortGroups
82
-	 * @return DataResponse
83
-	 */
84
-	public function index($pattern = '', $filterGroups = false, $sortGroups = MetaData::SORT_USERCOUNT) {
85
-		$groupPattern = $filterGroups ? $pattern : '';
76
+    /**
77
+     * @NoAdminRequired
78
+     *
79
+     * @param string $pattern
80
+     * @param bool $filterGroups
81
+     * @param int $sortGroups
82
+     * @return DataResponse
83
+     */
84
+    public function index($pattern = '', $filterGroups = false, $sortGroups = MetaData::SORT_USERCOUNT) {
85
+        $groupPattern = $filterGroups ? $pattern : '';
86 86
 
87
-		$groupsInfo = new MetaData(
88
-			$this->userSession->getUser()->getUID(),
89
-			$this->isAdmin,
90
-			$this->groupManager,
91
-			$this->userManager,
92
-			$this->userSession
93
-		);
94
-		$groupsInfo->setSorting($sortGroups);
95
-		list($adminGroups, $groups) = $groupsInfo->get($groupPattern, $pattern);
87
+        $groupsInfo = new MetaData(
88
+            $this->userSession->getUser()->getUID(),
89
+            $this->isAdmin,
90
+            $this->groupManager,
91
+            $this->userManager,
92
+            $this->userSession
93
+        );
94
+        $groupsInfo->setSorting($sortGroups);
95
+        list($adminGroups, $groups) = $groupsInfo->get($groupPattern, $pattern);
96 96
 
97
-		return new DataResponse(
98
-			array(
99
-				'data' => array('adminGroups' => $adminGroups, 'groups' => $groups)
100
-			)
101
-		);
102
-	}
97
+        return new DataResponse(
98
+            array(
99
+                'data' => array('adminGroups' => $adminGroups, 'groups' => $groups)
100
+            )
101
+        );
102
+    }
103 103
 
104
-	/**
105
-	 * @PasswordConfirmationRequired
106
-	 * @param string $id
107
-	 * @return DataResponse
108
-	 */
109
-	public function create($id) {
110
-		if($this->groupManager->groupExists($id)) {
111
-			return new DataResponse(
112
-				array(
113
-					'message' => (string)$this->l10n->t('Group already exists.')
114
-				),
115
-				Http::STATUS_CONFLICT
116
-			);
117
-		}
118
-		$group = $this->groupManager->createGroup($id);
119
-		if($group instanceof IGroup) {
120
-			return new DataResponse(['groupname' => $group->getDisplayName()], Http::STATUS_CREATED);
121
-		}
104
+    /**
105
+     * @PasswordConfirmationRequired
106
+     * @param string $id
107
+     * @return DataResponse
108
+     */
109
+    public function create($id) {
110
+        if($this->groupManager->groupExists($id)) {
111
+            return new DataResponse(
112
+                array(
113
+                    'message' => (string)$this->l10n->t('Group already exists.')
114
+                ),
115
+                Http::STATUS_CONFLICT
116
+            );
117
+        }
118
+        $group = $this->groupManager->createGroup($id);
119
+        if($group instanceof IGroup) {
120
+            return new DataResponse(['groupname' => $group->getDisplayName()], Http::STATUS_CREATED);
121
+        }
122 122
 
123
-		return new DataResponse(
124
-			array(
125
-				'status' => 'error',
126
-				'data' => array(
127
-					'message' => (string)$this->l10n->t('Unable to add group.')
128
-				)
129
-			),
130
-			Http::STATUS_FORBIDDEN
131
-		);
132
-	}
123
+        return new DataResponse(
124
+            array(
125
+                'status' => 'error',
126
+                'data' => array(
127
+                    'message' => (string)$this->l10n->t('Unable to add group.')
128
+                )
129
+            ),
130
+            Http::STATUS_FORBIDDEN
131
+        );
132
+    }
133 133
 
134
-	/**
135
-	 * @PasswordConfirmationRequired
136
-	 * @param string $id
137
-	 * @return DataResponse
138
-	 */
139
-	public function destroy($id) {
140
-		$group = $this->groupManager->get($id);
141
-		if ($group) {
142
-			if ($group->delete()) {
143
-				return new DataResponse(
144
-					array(
145
-						'status' => 'success',
146
-						'data' => ['groupname' => $group->getDisplayName()]
147
-					),
148
-					Http::STATUS_NO_CONTENT
149
-				);
150
-			}
151
-		}
152
-		return new DataResponse(
153
-			array(
154
-				'status' => 'error',
155
-				'data' => array(
156
-					'message' => (string)$this->l10n->t('Unable to delete group.')
157
-				),
158
-			),
159
-			Http::STATUS_FORBIDDEN
160
-		);
161
-	}
134
+    /**
135
+     * @PasswordConfirmationRequired
136
+     * @param string $id
137
+     * @return DataResponse
138
+     */
139
+    public function destroy($id) {
140
+        $group = $this->groupManager->get($id);
141
+        if ($group) {
142
+            if ($group->delete()) {
143
+                return new DataResponse(
144
+                    array(
145
+                        'status' => 'success',
146
+                        'data' => ['groupname' => $group->getDisplayName()]
147
+                    ),
148
+                    Http::STATUS_NO_CONTENT
149
+                );
150
+            }
151
+        }
152
+        return new DataResponse(
153
+            array(
154
+                'status' => 'error',
155
+                'data' => array(
156
+                    'message' => (string)$this->l10n->t('Unable to delete group.')
157
+                ),
158
+            ),
159
+            Http::STATUS_FORBIDDEN
160
+        );
161
+    }
162 162
 
163 163
 }
Please login to merge, or discard this patch.
lib/private/Group/Database.php 3 patches
Doc Comments   -1 removed lines patch added patch discarded remove patch
@@ -362,7 +362,6 @@
 block discarded – undo
362 362
 	/**
363 363
 	 * get the number of disabled users in a group
364 364
 	 *
365
-	 * @param string $search
366 365
 	 * @return int|bool
367 366
 	 */
368 367
 	public function countDisabledInGroup($gid) {
Please login to merge, or discard this patch.
Indentation   +337 added lines, -337 removed lines patch added patch discarded remove patch
@@ -47,343 +47,343 @@
 block discarded – undo
47 47
  */
48 48
 class Database extends Backend {
49 49
 
50
-	/** @var string[] */
51
-	private $groupCache = [];
52
-
53
-	/** @var IDBConnection */
54
-	private $dbConn;
55
-
56
-	/**
57
-	 * \OC\Group\Database constructor.
58
-	 *
59
-	 * @param IDBConnection|null $dbConn
60
-	 */
61
-	public function __construct(IDBConnection $dbConn = null) {
62
-		$this->dbConn = $dbConn;
63
-	}
64
-
65
-	/**
66
-	 * FIXME: This function should not be required!
67
-	 */
68
-	private function fixDI() {
69
-		if ($this->dbConn === null) {
70
-			$this->dbConn = \OC::$server->getDatabaseConnection();
71
-		}
72
-	}
73
-
74
-	/**
75
-	 * Try to create a new group
76
-	 * @param string $gid The name of the group to create
77
-	 * @return bool
78
-	 *
79
-	 * Tries to create a new group. If the group name already exists, false will
80
-	 * be returned.
81
-	 */
82
-	public function createGroup( $gid ) {
83
-		$this->fixDI();
84
-
85
-		// Add group
86
-		$result = $this->dbConn->insertIfNotExist('*PREFIX*groups', [
87
-			'gid' => $gid,
88
-		]);
89
-
90
-		// Add to cache
91
-		$this->groupCache[$gid] = $gid;
92
-
93
-		return $result === 1;
94
-	}
95
-
96
-	/**
97
-	 * delete a group
98
-	 * @param string $gid gid of the group to delete
99
-	 * @return bool
100
-	 *
101
-	 * Deletes a group and removes it from the group_user-table
102
-	 */
103
-	public function deleteGroup( $gid ) {
104
-		$this->fixDI();
105
-
106
-		// Delete the group
107
-		$qb = $this->dbConn->getQueryBuilder();
108
-		$qb->delete('groups')
109
-			->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
110
-			->execute();
111
-
112
-		// Delete the group-user relation
113
-		$qb = $this->dbConn->getQueryBuilder();
114
-		$qb->delete('group_user')
115
-			->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
116
-			->execute();
117
-
118
-		// Delete the group-groupadmin relation
119
-		$qb = $this->dbConn->getQueryBuilder();
120
-		$qb->delete('group_admin')
121
-			->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
122
-			->execute();
123
-
124
-		// Delete from cache
125
-		unset($this->groupCache[$gid]);
126
-
127
-		return true;
128
-	}
129
-
130
-	/**
131
-	 * is user in group?
132
-	 * @param string $uid uid of the user
133
-	 * @param string $gid gid of the group
134
-	 * @return bool
135
-	 *
136
-	 * Checks whether the user is member of a group or not.
137
-	 */
138
-	public function inGroup( $uid, $gid ) {
139
-		$this->fixDI();
140
-
141
-		// check
142
-		$qb = $this->dbConn->getQueryBuilder();
143
-		$cursor = $qb->select('uid')
144
-			->from('group_user')
145
-			->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
146
-			->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
147
-			->execute();
148
-
149
-		$result = $cursor->fetch();
150
-		$cursor->closeCursor();
151
-
152
-		return $result ? true : false;
153
-	}
154
-
155
-	/**
156
-	 * Add a user to a group
157
-	 * @param string $uid Name of the user to add to group
158
-	 * @param string $gid Name of the group in which add the user
159
-	 * @return bool
160
-	 *
161
-	 * Adds a user to a group.
162
-	 */
163
-	public function addToGroup( $uid, $gid ) {
164
-		$this->fixDI();
165
-
166
-		// No duplicate entries!
167
-		if( !$this->inGroup( $uid, $gid )) {
168
-			$qb = $this->dbConn->getQueryBuilder();
169
-			$qb->insert('group_user')
170
-				->setValue('uid', $qb->createNamedParameter($uid))
171
-				->setValue('gid', $qb->createNamedParameter($gid))
172
-				->execute();
173
-			return true;
174
-		}else{
175
-			return false;
176
-		}
177
-	}
178
-
179
-	/**
180
-	 * Removes a user from a group
181
-	 * @param string $uid Name of the user to remove from group
182
-	 * @param string $gid Name of the group from which remove the user
183
-	 * @return bool
184
-	 *
185
-	 * removes the user from a group.
186
-	 */
187
-	public function removeFromGroup( $uid, $gid ) {
188
-		$this->fixDI();
189
-
190
-		$qb = $this->dbConn->getQueryBuilder();
191
-		$qb->delete('group_user')
192
-			->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
193
-			->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
194
-			->execute();
195
-
196
-		return true;
197
-	}
198
-
199
-	/**
200
-	 * Get all groups a user belongs to
201
-	 * @param string $uid Name of the user
202
-	 * @return array an array of group names
203
-	 *
204
-	 * This function fetches all groups a user belongs to. It does not check
205
-	 * if the user exists at all.
206
-	 */
207
-	public function getUserGroups( $uid ) {
208
-		//guests has empty or null $uid
209
-		if ($uid === null || $uid === '') {
210
-			return [];
211
-		}
212
-
213
-		$this->fixDI();
214
-
215
-		// No magic!
216
-		$qb = $this->dbConn->getQueryBuilder();
217
-		$cursor = $qb->select('gid')
218
-			->from('group_user')
219
-			->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
220
-			->execute();
221
-
222
-		$groups = [];
223
-		while( $row = $cursor->fetch()) {
224
-			$groups[] = $row['gid'];
225
-			$this->groupCache[$row['gid']] = $row['gid'];
226
-		}
227
-		$cursor->closeCursor();
228
-
229
-		return $groups;
230
-	}
231
-
232
-	/**
233
-	 * get a list of all groups
234
-	 * @param string $search
235
-	 * @param int $limit
236
-	 * @param int $offset
237
-	 * @return array an array of group names
238
-	 *
239
-	 * Returns a list with all groups
240
-	 */
241
-	public function getGroups($search = '', $limit = null, $offset = null) {
242
-		$this->fixDI();
243
-
244
-		$query = $this->dbConn->getQueryBuilder();
245
-		$query->select('gid')
246
-			->from('groups')
247
-			->orderBy('gid', 'ASC');
248
-
249
-		if ($search !== '') {
250
-			$query->where($query->expr()->iLike('gid', $query->createNamedParameter(
251
-				'%' . $this->dbConn->escapeLikeParameter($search) . '%'
252
-			)));
253
-		}
254
-
255
-		$query->setMaxResults($limit)
256
-			->setFirstResult($offset);
257
-		$result = $query->execute();
258
-
259
-		$groups = [];
260
-		while ($row = $result->fetch()) {
261
-			$groups[] = $row['gid'];
262
-		}
263
-		$result->closeCursor();
264
-
265
-		return $groups;
266
-	}
267
-
268
-	/**
269
-	 * check if a group exists
270
-	 * @param string $gid
271
-	 * @return bool
272
-	 */
273
-	public function groupExists($gid) {
274
-		$this->fixDI();
275
-
276
-		// Check cache first
277
-		if (isset($this->groupCache[$gid])) {
278
-			return true;
279
-		}
280
-
281
-		$qb = $this->dbConn->getQueryBuilder();
282
-		$cursor = $qb->select('gid')
283
-			->from('groups')
284
-			->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
285
-			->execute();
286
-		$result = $cursor->fetch();
287
-		$cursor->closeCursor();
288
-
289
-		if ($result !== false) {
290
-			$this->groupCache[$gid] = $gid;
291
-			return true;
292
-		}
293
-		return false;
294
-	}
295
-
296
-	/**
297
-	 * get a list of all users in a group
298
-	 * @param string $gid
299
-	 * @param string $search
300
-	 * @param int $limit
301
-	 * @param int $offset
302
-	 * @return array an array of user ids
303
-	 */
304
-	public function usersInGroup($gid, $search = '', $limit = null, $offset = null) {
305
-		$this->fixDI();
306
-
307
-		$query = $this->dbConn->getQueryBuilder();
308
-		$query->select('uid')
309
-			->from('group_user')
310
-			->where($query->expr()->eq('gid', $query->createNamedParameter($gid)))
311
-			->orderBy('uid', 'ASC');
312
-
313
-		if ($search !== '') {
314
-			$query->andWhere($query->expr()->like('uid', $query->createNamedParameter(
315
-				'%' . $this->dbConn->escapeLikeParameter($search) . '%'
316
-			)));
317
-		}
318
-
319
-		$query->setMaxResults($limit)
320
-			->setFirstResult($offset);
321
-		$result = $query->execute();
322
-
323
-		$users = [];
324
-		while ($row = $result->fetch()) {
325
-			$users[] = $row['uid'];
326
-		}
327
-		$result->closeCursor();
328
-
329
-		return $users;
330
-	}
331
-
332
-	/**
333
-	 * get the number of all users matching the search string in a group
334
-	 * @param string $gid
335
-	 * @param string $search
336
-	 * @return int|false
337
-	 */
338
-	public function countUsersInGroup($gid, $search = '') {
339
-		$this->fixDI();
340
-
341
-		$query = $this->dbConn->getQueryBuilder();
342
-		$query->selectAlias($query->createFunction('COUNT(*)'), 'num_users')
343
-			->from('group_user')
344
-			->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
345
-
346
-		if ($search !== '') {
347
-			$query->andWhere($query->expr()->like('uid', $query->createNamedParameter(
348
-				'%' . $this->dbConn->escapeLikeParameter($search) . '%'
349
-			)));
350
-		}
351
-
352
-		$result = $query->execute();
353
-		$count = $result->fetchColumn();
354
-		$result->closeCursor();
355
-
356
-		if ($count !== false) {
357
-			$count = (int)$count;
358
-		}
359
-		return $count;
360
-	}
361
-
362
-	/**
363
-	 * get the number of disabled users in a group
364
-	 *
365
-	 * @param string $search
366
-	 * @return int|bool
367
-	 */
368
-	public function countDisabledInGroup($gid) {
369
-		$this->fixDI();
50
+    /** @var string[] */
51
+    private $groupCache = [];
52
+
53
+    /** @var IDBConnection */
54
+    private $dbConn;
55
+
56
+    /**
57
+     * \OC\Group\Database constructor.
58
+     *
59
+     * @param IDBConnection|null $dbConn
60
+     */
61
+    public function __construct(IDBConnection $dbConn = null) {
62
+        $this->dbConn = $dbConn;
63
+    }
64
+
65
+    /**
66
+     * FIXME: This function should not be required!
67
+     */
68
+    private function fixDI() {
69
+        if ($this->dbConn === null) {
70
+            $this->dbConn = \OC::$server->getDatabaseConnection();
71
+        }
72
+    }
73
+
74
+    /**
75
+     * Try to create a new group
76
+     * @param string $gid The name of the group to create
77
+     * @return bool
78
+     *
79
+     * Tries to create a new group. If the group name already exists, false will
80
+     * be returned.
81
+     */
82
+    public function createGroup( $gid ) {
83
+        $this->fixDI();
84
+
85
+        // Add group
86
+        $result = $this->dbConn->insertIfNotExist('*PREFIX*groups', [
87
+            'gid' => $gid,
88
+        ]);
89
+
90
+        // Add to cache
91
+        $this->groupCache[$gid] = $gid;
92
+
93
+        return $result === 1;
94
+    }
95
+
96
+    /**
97
+     * delete a group
98
+     * @param string $gid gid of the group to delete
99
+     * @return bool
100
+     *
101
+     * Deletes a group and removes it from the group_user-table
102
+     */
103
+    public function deleteGroup( $gid ) {
104
+        $this->fixDI();
105
+
106
+        // Delete the group
107
+        $qb = $this->dbConn->getQueryBuilder();
108
+        $qb->delete('groups')
109
+            ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
110
+            ->execute();
111
+
112
+        // Delete the group-user relation
113
+        $qb = $this->dbConn->getQueryBuilder();
114
+        $qb->delete('group_user')
115
+            ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
116
+            ->execute();
117
+
118
+        // Delete the group-groupadmin relation
119
+        $qb = $this->dbConn->getQueryBuilder();
120
+        $qb->delete('group_admin')
121
+            ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
122
+            ->execute();
123
+
124
+        // Delete from cache
125
+        unset($this->groupCache[$gid]);
126
+
127
+        return true;
128
+    }
129
+
130
+    /**
131
+     * is user in group?
132
+     * @param string $uid uid of the user
133
+     * @param string $gid gid of the group
134
+     * @return bool
135
+     *
136
+     * Checks whether the user is member of a group or not.
137
+     */
138
+    public function inGroup( $uid, $gid ) {
139
+        $this->fixDI();
140
+
141
+        // check
142
+        $qb = $this->dbConn->getQueryBuilder();
143
+        $cursor = $qb->select('uid')
144
+            ->from('group_user')
145
+            ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
146
+            ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
147
+            ->execute();
148
+
149
+        $result = $cursor->fetch();
150
+        $cursor->closeCursor();
151
+
152
+        return $result ? true : false;
153
+    }
154
+
155
+    /**
156
+     * Add a user to a group
157
+     * @param string $uid Name of the user to add to group
158
+     * @param string $gid Name of the group in which add the user
159
+     * @return bool
160
+     *
161
+     * Adds a user to a group.
162
+     */
163
+    public function addToGroup( $uid, $gid ) {
164
+        $this->fixDI();
165
+
166
+        // No duplicate entries!
167
+        if( !$this->inGroup( $uid, $gid )) {
168
+            $qb = $this->dbConn->getQueryBuilder();
169
+            $qb->insert('group_user')
170
+                ->setValue('uid', $qb->createNamedParameter($uid))
171
+                ->setValue('gid', $qb->createNamedParameter($gid))
172
+                ->execute();
173
+            return true;
174
+        }else{
175
+            return false;
176
+        }
177
+    }
178
+
179
+    /**
180
+     * Removes a user from a group
181
+     * @param string $uid Name of the user to remove from group
182
+     * @param string $gid Name of the group from which remove the user
183
+     * @return bool
184
+     *
185
+     * removes the user from a group.
186
+     */
187
+    public function removeFromGroup( $uid, $gid ) {
188
+        $this->fixDI();
189
+
190
+        $qb = $this->dbConn->getQueryBuilder();
191
+        $qb->delete('group_user')
192
+            ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
193
+            ->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
194
+            ->execute();
195
+
196
+        return true;
197
+    }
198
+
199
+    /**
200
+     * Get all groups a user belongs to
201
+     * @param string $uid Name of the user
202
+     * @return array an array of group names
203
+     *
204
+     * This function fetches all groups a user belongs to. It does not check
205
+     * if the user exists at all.
206
+     */
207
+    public function getUserGroups( $uid ) {
208
+        //guests has empty or null $uid
209
+        if ($uid === null || $uid === '') {
210
+            return [];
211
+        }
212
+
213
+        $this->fixDI();
214
+
215
+        // No magic!
216
+        $qb = $this->dbConn->getQueryBuilder();
217
+        $cursor = $qb->select('gid')
218
+            ->from('group_user')
219
+            ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
220
+            ->execute();
221
+
222
+        $groups = [];
223
+        while( $row = $cursor->fetch()) {
224
+            $groups[] = $row['gid'];
225
+            $this->groupCache[$row['gid']] = $row['gid'];
226
+        }
227
+        $cursor->closeCursor();
228
+
229
+        return $groups;
230
+    }
231
+
232
+    /**
233
+     * get a list of all groups
234
+     * @param string $search
235
+     * @param int $limit
236
+     * @param int $offset
237
+     * @return array an array of group names
238
+     *
239
+     * Returns a list with all groups
240
+     */
241
+    public function getGroups($search = '', $limit = null, $offset = null) {
242
+        $this->fixDI();
243
+
244
+        $query = $this->dbConn->getQueryBuilder();
245
+        $query->select('gid')
246
+            ->from('groups')
247
+            ->orderBy('gid', 'ASC');
248
+
249
+        if ($search !== '') {
250
+            $query->where($query->expr()->iLike('gid', $query->createNamedParameter(
251
+                '%' . $this->dbConn->escapeLikeParameter($search) . '%'
252
+            )));
253
+        }
254
+
255
+        $query->setMaxResults($limit)
256
+            ->setFirstResult($offset);
257
+        $result = $query->execute();
258
+
259
+        $groups = [];
260
+        while ($row = $result->fetch()) {
261
+            $groups[] = $row['gid'];
262
+        }
263
+        $result->closeCursor();
264
+
265
+        return $groups;
266
+    }
267
+
268
+    /**
269
+     * check if a group exists
270
+     * @param string $gid
271
+     * @return bool
272
+     */
273
+    public function groupExists($gid) {
274
+        $this->fixDI();
275
+
276
+        // Check cache first
277
+        if (isset($this->groupCache[$gid])) {
278
+            return true;
279
+        }
280
+
281
+        $qb = $this->dbConn->getQueryBuilder();
282
+        $cursor = $qb->select('gid')
283
+            ->from('groups')
284
+            ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
285
+            ->execute();
286
+        $result = $cursor->fetch();
287
+        $cursor->closeCursor();
288
+
289
+        if ($result !== false) {
290
+            $this->groupCache[$gid] = $gid;
291
+            return true;
292
+        }
293
+        return false;
294
+    }
295
+
296
+    /**
297
+     * get a list of all users in a group
298
+     * @param string $gid
299
+     * @param string $search
300
+     * @param int $limit
301
+     * @param int $offset
302
+     * @return array an array of user ids
303
+     */
304
+    public function usersInGroup($gid, $search = '', $limit = null, $offset = null) {
305
+        $this->fixDI();
306
+
307
+        $query = $this->dbConn->getQueryBuilder();
308
+        $query->select('uid')
309
+            ->from('group_user')
310
+            ->where($query->expr()->eq('gid', $query->createNamedParameter($gid)))
311
+            ->orderBy('uid', 'ASC');
312
+
313
+        if ($search !== '') {
314
+            $query->andWhere($query->expr()->like('uid', $query->createNamedParameter(
315
+                '%' . $this->dbConn->escapeLikeParameter($search) . '%'
316
+            )));
317
+        }
318
+
319
+        $query->setMaxResults($limit)
320
+            ->setFirstResult($offset);
321
+        $result = $query->execute();
322
+
323
+        $users = [];
324
+        while ($row = $result->fetch()) {
325
+            $users[] = $row['uid'];
326
+        }
327
+        $result->closeCursor();
328
+
329
+        return $users;
330
+    }
331
+
332
+    /**
333
+     * get the number of all users matching the search string in a group
334
+     * @param string $gid
335
+     * @param string $search
336
+     * @return int|false
337
+     */
338
+    public function countUsersInGroup($gid, $search = '') {
339
+        $this->fixDI();
340
+
341
+        $query = $this->dbConn->getQueryBuilder();
342
+        $query->selectAlias($query->createFunction('COUNT(*)'), 'num_users')
343
+            ->from('group_user')
344
+            ->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
345
+
346
+        if ($search !== '') {
347
+            $query->andWhere($query->expr()->like('uid', $query->createNamedParameter(
348
+                '%' . $this->dbConn->escapeLikeParameter($search) . '%'
349
+            )));
350
+        }
351
+
352
+        $result = $query->execute();
353
+        $count = $result->fetchColumn();
354
+        $result->closeCursor();
355
+
356
+        if ($count !== false) {
357
+            $count = (int)$count;
358
+        }
359
+        return $count;
360
+    }
361
+
362
+    /**
363
+     * get the number of disabled users in a group
364
+     *
365
+     * @param string $search
366
+     * @return int|bool
367
+     */
368
+    public function countDisabledInGroup($gid) {
369
+        $this->fixDI();
370 370
 		
371
-		$query = $this->dbConn->getQueryBuilder();
372
-		$query->select($queryBuilder->createFunction('COUNT(*)'))
373
-			->from('preferences')
374
-			->where($query->expr()->eq('appid', $queryBuilder->createNamedParameter('core')))
375
-			->andWhere($query->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled')))
376
-			->andWhere($query->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR));
377
-
378
-		$result = $query->execute();
379
-		$result = (int)$result->fetchColumn();
380
-		$query->closeCursor();
381
-
382
-
383
-		if ($count !== false) {
384
-			$count = (int)$count;
385
-		}
386
-		return $count;
387
-	}
371
+        $query = $this->dbConn->getQueryBuilder();
372
+        $query->select($queryBuilder->createFunction('COUNT(*)'))
373
+            ->from('preferences')
374
+            ->where($query->expr()->eq('appid', $queryBuilder->createNamedParameter('core')))
375
+            ->andWhere($query->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled')))
376
+            ->andWhere($query->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR));
377
+
378
+        $result = $query->execute();
379
+        $result = (int)$result->fetchColumn();
380
+        $query->closeCursor();
381
+
382
+
383
+        if ($count !== false) {
384
+            $count = (int)$count;
385
+        }
386
+        return $count;
387
+    }
388 388
 
389 389
 }
Please login to merge, or discard this patch.
Spacing   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -79,7 +79,7 @@  discard block
 block discarded – undo
79 79
 	 * Tries to create a new group. If the group name already exists, false will
80 80
 	 * be returned.
81 81
 	 */
82
-	public function createGroup( $gid ) {
82
+	public function createGroup($gid) {
83 83
 		$this->fixDI();
84 84
 
85 85
 		// Add group
@@ -100,7 +100,7 @@  discard block
 block discarded – undo
100 100
 	 *
101 101
 	 * Deletes a group and removes it from the group_user-table
102 102
 	 */
103
-	public function deleteGroup( $gid ) {
103
+	public function deleteGroup($gid) {
104 104
 		$this->fixDI();
105 105
 
106 106
 		// Delete the group
@@ -135,7 +135,7 @@  discard block
 block discarded – undo
135 135
 	 *
136 136
 	 * Checks whether the user is member of a group or not.
137 137
 	 */
138
-	public function inGroup( $uid, $gid ) {
138
+	public function inGroup($uid, $gid) {
139 139
 		$this->fixDI();
140 140
 
141 141
 		// check
@@ -160,18 +160,18 @@  discard block
 block discarded – undo
160 160
 	 *
161 161
 	 * Adds a user to a group.
162 162
 	 */
163
-	public function addToGroup( $uid, $gid ) {
163
+	public function addToGroup($uid, $gid) {
164 164
 		$this->fixDI();
165 165
 
166 166
 		// No duplicate entries!
167
-		if( !$this->inGroup( $uid, $gid )) {
167
+		if (!$this->inGroup($uid, $gid)) {
168 168
 			$qb = $this->dbConn->getQueryBuilder();
169 169
 			$qb->insert('group_user')
170 170
 				->setValue('uid', $qb->createNamedParameter($uid))
171 171
 				->setValue('gid', $qb->createNamedParameter($gid))
172 172
 				->execute();
173 173
 			return true;
174
-		}else{
174
+		} else {
175 175
 			return false;
176 176
 		}
177 177
 	}
@@ -184,7 +184,7 @@  discard block
 block discarded – undo
184 184
 	 *
185 185
 	 * removes the user from a group.
186 186
 	 */
187
-	public function removeFromGroup( $uid, $gid ) {
187
+	public function removeFromGroup($uid, $gid) {
188 188
 		$this->fixDI();
189 189
 
190 190
 		$qb = $this->dbConn->getQueryBuilder();
@@ -204,7 +204,7 @@  discard block
 block discarded – undo
204 204
 	 * This function fetches all groups a user belongs to. It does not check
205 205
 	 * if the user exists at all.
206 206
 	 */
207
-	public function getUserGroups( $uid ) {
207
+	public function getUserGroups($uid) {
208 208
 		//guests has empty or null $uid
209 209
 		if ($uid === null || $uid === '') {
210 210
 			return [];
@@ -220,7 +220,7 @@  discard block
 block discarded – undo
220 220
 			->execute();
221 221
 
222 222
 		$groups = [];
223
-		while( $row = $cursor->fetch()) {
223
+		while ($row = $cursor->fetch()) {
224 224
 			$groups[] = $row['gid'];
225 225
 			$this->groupCache[$row['gid']] = $row['gid'];
226 226
 		}
@@ -248,7 +248,7 @@  discard block
 block discarded – undo
248 248
 
249 249
 		if ($search !== '') {
250 250
 			$query->where($query->expr()->iLike('gid', $query->createNamedParameter(
251
-				'%' . $this->dbConn->escapeLikeParameter($search) . '%'
251
+				'%'.$this->dbConn->escapeLikeParameter($search).'%'
252 252
 			)));
253 253
 		}
254 254
 
@@ -312,7 +312,7 @@  discard block
 block discarded – undo
312 312
 
313 313
 		if ($search !== '') {
314 314
 			$query->andWhere($query->expr()->like('uid', $query->createNamedParameter(
315
-				'%' . $this->dbConn->escapeLikeParameter($search) . '%'
315
+				'%'.$this->dbConn->escapeLikeParameter($search).'%'
316 316
 			)));
317 317
 		}
318 318
 
@@ -345,7 +345,7 @@  discard block
 block discarded – undo
345 345
 
346 346
 		if ($search !== '') {
347 347
 			$query->andWhere($query->expr()->like('uid', $query->createNamedParameter(
348
-				'%' . $this->dbConn->escapeLikeParameter($search) . '%'
348
+				'%'.$this->dbConn->escapeLikeParameter($search).'%'
349 349
 			)));
350 350
 		}
351 351
 
@@ -354,7 +354,7 @@  discard block
 block discarded – undo
354 354
 		$result->closeCursor();
355 355
 
356 356
 		if ($count !== false) {
357
-			$count = (int)$count;
357
+			$count = (int) $count;
358 358
 		}
359 359
 		return $count;
360 360
 	}
@@ -376,12 +376,12 @@  discard block
 block discarded – undo
376 376
 			->andWhere($query->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR));
377 377
 
378 378
 		$result = $query->execute();
379
-		$result = (int)$result->fetchColumn();
379
+		$result = (int) $result->fetchColumn();
380 380
 		$query->closeCursor();
381 381
 
382 382
 
383 383
 		if ($count !== false) {
384
-			$count = (int)$count;
384
+			$count = (int) $count;
385 385
 		}
386 386
 		return $count;
387 387
 	}
Please login to merge, or discard this patch.
lib/private/Group/Backend.php 1 patch
Indentation   +94 added lines, -94 removed lines patch added patch discarded remove patch
@@ -28,108 +28,108 @@
 block discarded – undo
28 28
  * Abstract base class for user management
29 29
  */
30 30
 abstract class Backend implements \OCP\GroupInterface {
31
-	/**
32
-	 * error code for functions not provided by the group backend
33
-	 */
34
-	const NOT_IMPLEMENTED = -501;
31
+    /**
32
+     * error code for functions not provided by the group backend
33
+     */
34
+    const NOT_IMPLEMENTED = -501;
35 35
 
36
-	protected $possibleActions = [
37
-		self::CREATE_GROUP => 'createGroup',
38
-		self::DELETE_GROUP => 'deleteGroup',
39
-		self::ADD_TO_GROUP => 'addToGroup',
40
-		self::REMOVE_FROM_GOUP => 'removeFromGroup',
41
-		self::COUNT_USERS => 'countUsersInGroup',
42
-		self::COUNT_DISABLED => 'countDisabledInGroup',
43
-		self::GROUP_DETAILS => 'getGroupDetails',
44
-		self::IS_ADMIN => 'isAdmin',
45
-	];
36
+    protected $possibleActions = [
37
+        self::CREATE_GROUP => 'createGroup',
38
+        self::DELETE_GROUP => 'deleteGroup',
39
+        self::ADD_TO_GROUP => 'addToGroup',
40
+        self::REMOVE_FROM_GOUP => 'removeFromGroup',
41
+        self::COUNT_USERS => 'countUsersInGroup',
42
+        self::COUNT_DISABLED => 'countDisabledInGroup',
43
+        self::GROUP_DETAILS => 'getGroupDetails',
44
+        self::IS_ADMIN => 'isAdmin',
45
+    ];
46 46
 
47
-	/**
48
-	* Get all supported actions
49
-	* @return int bitwise-or'ed actions
50
-	*
51
-	* Returns the supported actions as int to be
52
-	* compared with \OC\Group\Backend::CREATE_GROUP etc.
53
-	*/
54
-	public function getSupportedActions() {
55
-		$actions = 0;
56
-		foreach($this->possibleActions AS $action => $methodName) {
57
-			if(method_exists($this, $methodName)) {
58
-				$actions |= $action;
59
-			}
60
-		}
47
+    /**
48
+     * Get all supported actions
49
+     * @return int bitwise-or'ed actions
50
+     *
51
+     * Returns the supported actions as int to be
52
+     * compared with \OC\Group\Backend::CREATE_GROUP etc.
53
+     */
54
+    public function getSupportedActions() {
55
+        $actions = 0;
56
+        foreach($this->possibleActions AS $action => $methodName) {
57
+            if(method_exists($this, $methodName)) {
58
+                $actions |= $action;
59
+            }
60
+        }
61 61
 
62
-		return $actions;
63
-	}
62
+        return $actions;
63
+    }
64 64
 
65
-	/**
66
-	* Check if backend implements actions
67
-	* @param int $actions bitwise-or'ed actions
68
-	* @return bool
69
-	*
70
-	* Returns the supported actions as int to be
71
-	* compared with \OC\Group\Backend::CREATE_GROUP etc.
72
-	*/
73
-	public function implementsActions($actions) {
74
-		return (bool)($this->getSupportedActions() & $actions);
75
-	}
65
+    /**
66
+     * Check if backend implements actions
67
+     * @param int $actions bitwise-or'ed actions
68
+     * @return bool
69
+     *
70
+     * Returns the supported actions as int to be
71
+     * compared with \OC\Group\Backend::CREATE_GROUP etc.
72
+     */
73
+    public function implementsActions($actions) {
74
+        return (bool)($this->getSupportedActions() & $actions);
75
+    }
76 76
 
77
-	/**
78
-	 * is user in group?
79
-	 * @param string $uid uid of the user
80
-	 * @param string $gid gid of the group
81
-	 * @return bool
82
-	 *
83
-	 * Checks whether the user is member of a group or not.
84
-	 */
85
-	public function inGroup($uid, $gid) {
86
-		return in_array($gid, $this->getUserGroups($uid));
87
-	}
77
+    /**
78
+     * is user in group?
79
+     * @param string $uid uid of the user
80
+     * @param string $gid gid of the group
81
+     * @return bool
82
+     *
83
+     * Checks whether the user is member of a group or not.
84
+     */
85
+    public function inGroup($uid, $gid) {
86
+        return in_array($gid, $this->getUserGroups($uid));
87
+    }
88 88
 
89
-	/**
90
-	 * Get all groups a user belongs to
91
-	 * @param string $uid Name of the user
92
-	 * @return array an array of group names
93
-	 *
94
-	 * This function fetches all groups a user belongs to. It does not check
95
-	 * if the user exists at all.
96
-	 */
97
-	public function getUserGroups($uid) {
98
-		return array();
99
-	}
89
+    /**
90
+     * Get all groups a user belongs to
91
+     * @param string $uid Name of the user
92
+     * @return array an array of group names
93
+     *
94
+     * This function fetches all groups a user belongs to. It does not check
95
+     * if the user exists at all.
96
+     */
97
+    public function getUserGroups($uid) {
98
+        return array();
99
+    }
100 100
 
101
-	/**
102
-	 * get a list of all groups
103
-	 * @param string $search
104
-	 * @param int $limit
105
-	 * @param int $offset
106
-	 * @return array an array of group names
107
-	 *
108
-	 * Returns a list with all groups
109
-	 */
101
+    /**
102
+     * get a list of all groups
103
+     * @param string $search
104
+     * @param int $limit
105
+     * @param int $offset
106
+     * @return array an array of group names
107
+     *
108
+     * Returns a list with all groups
109
+     */
110 110
 
111
-	public function getGroups($search = '', $limit = -1, $offset = 0) {
112
-		return array();
113
-	}
111
+    public function getGroups($search = '', $limit = -1, $offset = 0) {
112
+        return array();
113
+    }
114 114
 
115
-	/**
116
-	 * check if a group exists
117
-	 * @param string $gid
118
-	 * @return bool
119
-	 */
120
-	public function groupExists($gid) {
121
-		return in_array($gid, $this->getGroups($gid, 1));
122
-	}
115
+    /**
116
+     * check if a group exists
117
+     * @param string $gid
118
+     * @return bool
119
+     */
120
+    public function groupExists($gid) {
121
+        return in_array($gid, $this->getGroups($gid, 1));
122
+    }
123 123
 
124
-	/**
125
-	 * get a list of all users in a group
126
-	 * @param string $gid
127
-	 * @param string $search
128
-	 * @param int $limit
129
-	 * @param int $offset
130
-	 * @return array an array of user ids
131
-	 */
132
-	public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
133
-		return array();
134
-	}
124
+    /**
125
+     * get a list of all users in a group
126
+     * @param string $gid
127
+     * @param string $search
128
+     * @param int $limit
129
+     * @param int $offset
130
+     * @return array an array of user ids
131
+     */
132
+    public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
133
+        return array();
134
+    }
135 135
 }
Please login to merge, or discard this patch.
lib/private/Group/Group.php 2 patches
Indentation   +265 added lines, -265 removed lines patch added patch discarded remove patch
@@ -33,292 +33,292 @@
 block discarded – undo
33 33
 use OCP\IUser;
34 34
 
35 35
 class Group implements IGroup {
36
-	/** @var null|string  */
37
-	protected $displayName;
36
+    /** @var null|string  */
37
+    protected $displayName;
38 38
 
39
-	/**
40
-	 * @var string $id
41
-	 */
42
-	private $gid;
39
+    /**
40
+     * @var string $id
41
+     */
42
+    private $gid;
43 43
 
44
-	/**
45
-	 * @var \OC\User\User[] $users
46
-	 */
47
-	private $users = array();
44
+    /**
45
+     * @var \OC\User\User[] $users
46
+     */
47
+    private $users = array();
48 48
 
49
-	/**
50
-	 * @var bool $usersLoaded
51
-	 */
52
-	private $usersLoaded;
49
+    /**
50
+     * @var bool $usersLoaded
51
+     */
52
+    private $usersLoaded;
53 53
 
54
-	/**
55
-	 * @var \OC\Group\Backend[]|\OC\Group\Database[] $backend
56
-	 */
57
-	private $backends;
54
+    /**
55
+     * @var \OC\Group\Backend[]|\OC\Group\Database[] $backend
56
+     */
57
+    private $backends;
58 58
 
59
-	/**
60
-	 * @var \OC\Hooks\PublicEmitter $emitter
61
-	 */
62
-	private $emitter;
59
+    /**
60
+     * @var \OC\Hooks\PublicEmitter $emitter
61
+     */
62
+    private $emitter;
63 63
 
64
-	/**
65
-	 * @var \OC\User\Manager $userManager
66
-	 */
67
-	private $userManager;
64
+    /**
65
+     * @var \OC\User\Manager $userManager
66
+     */
67
+    private $userManager;
68 68
 
69
-	/**
70
-	 * @param string $gid
71
-	 * @param \OC\Group\Backend[] $backends
72
-	 * @param \OC\User\Manager $userManager
73
-	 * @param \OC\Hooks\PublicEmitter $emitter
74
-	 * @param string $displayName
75
-	 */
76
-	public function __construct($gid, $backends, $userManager, $emitter = null, $displayName = null) {
77
-		$this->gid = $gid;
78
-		$this->backends = $backends;
79
-		$this->userManager = $userManager;
80
-		$this->emitter = $emitter;
81
-		$this->displayName = $displayName;
82
-	}
69
+    /**
70
+     * @param string $gid
71
+     * @param \OC\Group\Backend[] $backends
72
+     * @param \OC\User\Manager $userManager
73
+     * @param \OC\Hooks\PublicEmitter $emitter
74
+     * @param string $displayName
75
+     */
76
+    public function __construct($gid, $backends, $userManager, $emitter = null, $displayName = null) {
77
+        $this->gid = $gid;
78
+        $this->backends = $backends;
79
+        $this->userManager = $userManager;
80
+        $this->emitter = $emitter;
81
+        $this->displayName = $displayName;
82
+    }
83 83
 
84
-	public function getGID() {
85
-		return $this->gid;
86
-	}
84
+    public function getGID() {
85
+        return $this->gid;
86
+    }
87 87
 
88
-	public function getDisplayName() {
89
-		if (is_null($this->displayName)) {
90
-			return $this->gid;
91
-		}
92
-		return $this->displayName;
93
-	}
88
+    public function getDisplayName() {
89
+        if (is_null($this->displayName)) {
90
+            return $this->gid;
91
+        }
92
+        return $this->displayName;
93
+    }
94 94
 
95
-	/**
96
-	 * get all users in the group
97
-	 *
98
-	 * @return \OC\User\User[]
99
-	 */
100
-	public function getUsers() {
101
-		if ($this->usersLoaded) {
102
-			return $this->users;
103
-		}
95
+    /**
96
+     * get all users in the group
97
+     *
98
+     * @return \OC\User\User[]
99
+     */
100
+    public function getUsers() {
101
+        if ($this->usersLoaded) {
102
+            return $this->users;
103
+        }
104 104
 
105
-		$userIds = array();
106
-		foreach ($this->backends as $backend) {
107
-			$diff = array_diff(
108
-				$backend->usersInGroup($this->gid),
109
-				$userIds
110
-			);
111
-			if ($diff) {
112
-				$userIds = array_merge($userIds, $diff);
113
-			}
114
-		}
105
+        $userIds = array();
106
+        foreach ($this->backends as $backend) {
107
+            $diff = array_diff(
108
+                $backend->usersInGroup($this->gid),
109
+                $userIds
110
+            );
111
+            if ($diff) {
112
+                $userIds = array_merge($userIds, $diff);
113
+            }
114
+        }
115 115
 
116
-		$this->users = $this->getVerifiedUsers($userIds);
117
-		$this->usersLoaded = true;
118
-		return $this->users;
119
-	}
116
+        $this->users = $this->getVerifiedUsers($userIds);
117
+        $this->usersLoaded = true;
118
+        return $this->users;
119
+    }
120 120
 
121
-	/**
122
-	 * check if a user is in the group
123
-	 *
124
-	 * @param IUser $user
125
-	 * @return bool
126
-	 */
127
-	public function inGroup(IUser $user) {
128
-		if (isset($this->users[$user->getUID()])) {
129
-			return true;
130
-		}
131
-		foreach ($this->backends as $backend) {
132
-			if ($backend->inGroup($user->getUID(), $this->gid)) {
133
-				$this->users[$user->getUID()] = $user;
134
-				return true;
135
-			}
136
-		}
137
-		return false;
138
-	}
121
+    /**
122
+     * check if a user is in the group
123
+     *
124
+     * @param IUser $user
125
+     * @return bool
126
+     */
127
+    public function inGroup(IUser $user) {
128
+        if (isset($this->users[$user->getUID()])) {
129
+            return true;
130
+        }
131
+        foreach ($this->backends as $backend) {
132
+            if ($backend->inGroup($user->getUID(), $this->gid)) {
133
+                $this->users[$user->getUID()] = $user;
134
+                return true;
135
+            }
136
+        }
137
+        return false;
138
+    }
139 139
 
140
-	/**
141
-	 * add a user to the group
142
-	 *
143
-	 * @param IUser $user
144
-	 */
145
-	public function addUser(IUser $user) {
146
-		if ($this->inGroup($user)) {
147
-			return;
148
-		}
140
+    /**
141
+     * add a user to the group
142
+     *
143
+     * @param IUser $user
144
+     */
145
+    public function addUser(IUser $user) {
146
+        if ($this->inGroup($user)) {
147
+            return;
148
+        }
149 149
 
150
-		if ($this->emitter) {
151
-			$this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user));
152
-		}
153
-		foreach ($this->backends as $backend) {
154
-			if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) {
155
-				$backend->addToGroup($user->getUID(), $this->gid);
156
-				if ($this->users) {
157
-					$this->users[$user->getUID()] = $user;
158
-				}
159
-				if ($this->emitter) {
160
-					$this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user));
161
-				}
162
-				return;
163
-			}
164
-		}
165
-	}
150
+        if ($this->emitter) {
151
+            $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user));
152
+        }
153
+        foreach ($this->backends as $backend) {
154
+            if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) {
155
+                $backend->addToGroup($user->getUID(), $this->gid);
156
+                if ($this->users) {
157
+                    $this->users[$user->getUID()] = $user;
158
+                }
159
+                if ($this->emitter) {
160
+                    $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user));
161
+                }
162
+                return;
163
+            }
164
+        }
165
+    }
166 166
 
167
-	/**
168
-	 * remove a user from the group
169
-	 *
170
-	 * @param \OC\User\User $user
171
-	 */
172
-	public function removeUser($user) {
173
-		$result = false;
174
-		if ($this->emitter) {
175
-			$this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user));
176
-		}
177
-		foreach ($this->backends as $backend) {
178
-			if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) {
179
-				$backend->removeFromGroup($user->getUID(), $this->gid);
180
-				$result = true;
181
-			}
182
-		}
183
-		if ($result) {
184
-			if ($this->emitter) {
185
-				$this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user));
186
-			}
187
-			if ($this->users) {
188
-				foreach ($this->users as $index => $groupUser) {
189
-					if ($groupUser->getUID() === $user->getUID()) {
190
-						unset($this->users[$index]);
191
-						return;
192
-					}
193
-				}
194
-			}
195
-		}
196
-	}
167
+    /**
168
+     * remove a user from the group
169
+     *
170
+     * @param \OC\User\User $user
171
+     */
172
+    public function removeUser($user) {
173
+        $result = false;
174
+        if ($this->emitter) {
175
+            $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user));
176
+        }
177
+        foreach ($this->backends as $backend) {
178
+            if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) {
179
+                $backend->removeFromGroup($user->getUID(), $this->gid);
180
+                $result = true;
181
+            }
182
+        }
183
+        if ($result) {
184
+            if ($this->emitter) {
185
+                $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user));
186
+            }
187
+            if ($this->users) {
188
+                foreach ($this->users as $index => $groupUser) {
189
+                    if ($groupUser->getUID() === $user->getUID()) {
190
+                        unset($this->users[$index]);
191
+                        return;
192
+                    }
193
+                }
194
+            }
195
+        }
196
+    }
197 197
 
198
-	/**
199
-	 * search for users in the group by userid
200
-	 *
201
-	 * @param string $search
202
-	 * @param int $limit
203
-	 * @param int $offset
204
-	 * @return \OC\User\User[]
205
-	 */
206
-	public function searchUsers($search, $limit = null, $offset = null) {
207
-		$users = array();
208
-		foreach ($this->backends as $backend) {
209
-			$userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
210
-			$users += $this->getVerifiedUsers($userIds);
211
-			if (!is_null($limit) and $limit <= 0) {
212
-				return array_values($users);
213
-			}
214
-		}
215
-		return array_values($users);
216
-	}
198
+    /**
199
+     * search for users in the group by userid
200
+     *
201
+     * @param string $search
202
+     * @param int $limit
203
+     * @param int $offset
204
+     * @return \OC\User\User[]
205
+     */
206
+    public function searchUsers($search, $limit = null, $offset = null) {
207
+        $users = array();
208
+        foreach ($this->backends as $backend) {
209
+            $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
210
+            $users += $this->getVerifiedUsers($userIds);
211
+            if (!is_null($limit) and $limit <= 0) {
212
+                return array_values($users);
213
+            }
214
+        }
215
+        return array_values($users);
216
+    }
217 217
 
218
-	/**
219
-	 * returns the number of users matching the search string
220
-	 *
221
-	 * @param string $search
222
-	 * @return int|bool
223
-	 */
224
-	public function count($search = '') {
225
-		$users = false;
226
-		foreach ($this->backends as $backend) {
227
-			if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) {
228
-				if($users === false) {
229
-					//we could directly add to a bool variable, but this would
230
-					//be ugly
231
-					$users = 0;
232
-				}
233
-				$users += $backend->countUsersInGroup($this->gid, $search);
234
-			}
235
-		}
236
-		return $users;
237
-	}
218
+    /**
219
+     * returns the number of users matching the search string
220
+     *
221
+     * @param string $search
222
+     * @return int|bool
223
+     */
224
+    public function count($search = '') {
225
+        $users = false;
226
+        foreach ($this->backends as $backend) {
227
+            if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) {
228
+                if($users === false) {
229
+                    //we could directly add to a bool variable, but this would
230
+                    //be ugly
231
+                    $users = 0;
232
+                }
233
+                $users += $backend->countUsersInGroup($this->gid, $search);
234
+            }
235
+        }
236
+        return $users;
237
+    }
238 238
 
239
-	/**
240
-	 * returns the number of disabled users
241
-	 *
242
-	 * @return int|bool
243
-	 */
244
-	public function countDisabled() {
245
-		$users = false;
246
-		foreach ($this->backends as $backend) {
247
-			if($backend->implementsActions(\OC\Group\Backend::COUNT_DISABLED)) {
248
-				if($users === false) {
249
-					//we could directly add to a bool variable, but this would
250
-					//be ugly
251
-					$users = 0;
252
-				}
253
-				$users += $backend->countUsersInGroup($this->gid);
254
-			}
255
-		}
256
-		return $users;
257
-	}
239
+    /**
240
+     * returns the number of disabled users
241
+     *
242
+     * @return int|bool
243
+     */
244
+    public function countDisabled() {
245
+        $users = false;
246
+        foreach ($this->backends as $backend) {
247
+            if($backend->implementsActions(\OC\Group\Backend::COUNT_DISABLED)) {
248
+                if($users === false) {
249
+                    //we could directly add to a bool variable, but this would
250
+                    //be ugly
251
+                    $users = 0;
252
+                }
253
+                $users += $backend->countUsersInGroup($this->gid);
254
+            }
255
+        }
256
+        return $users;
257
+    }
258 258
 
259
-	/**
260
-	 * search for users in the group by displayname
261
-	 *
262
-	 * @param string $search
263
-	 * @param int $limit
264
-	 * @param int $offset
265
-	 * @return \OC\User\User[]
266
-	 */
267
-	public function searchDisplayName($search, $limit = null, $offset = null) {
268
-		$users = array();
269
-		foreach ($this->backends as $backend) {
270
-			$userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
271
-			$users = $this->getVerifiedUsers($userIds);
272
-			if (!is_null($limit) and $limit <= 0) {
273
-				return array_values($users);
274
-			}
275
-		}
276
-		return array_values($users);
277
-	}
259
+    /**
260
+     * search for users in the group by displayname
261
+     *
262
+     * @param string $search
263
+     * @param int $limit
264
+     * @param int $offset
265
+     * @return \OC\User\User[]
266
+     */
267
+    public function searchDisplayName($search, $limit = null, $offset = null) {
268
+        $users = array();
269
+        foreach ($this->backends as $backend) {
270
+            $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
271
+            $users = $this->getVerifiedUsers($userIds);
272
+            if (!is_null($limit) and $limit <= 0) {
273
+                return array_values($users);
274
+            }
275
+        }
276
+        return array_values($users);
277
+    }
278 278
 
279
-	/**
280
-	 * delete the group
281
-	 *
282
-	 * @return bool
283
-	 */
284
-	public function delete() {
285
-		// Prevent users from deleting group admin
286
-		if ($this->getGID() === 'admin') {
287
-			return false;
288
-		}
279
+    /**
280
+     * delete the group
281
+     *
282
+     * @return bool
283
+     */
284
+    public function delete() {
285
+        // Prevent users from deleting group admin
286
+        if ($this->getGID() === 'admin') {
287
+            return false;
288
+        }
289 289
 
290
-		$result = false;
291
-		if ($this->emitter) {
292
-			$this->emitter->emit('\OC\Group', 'preDelete', array($this));
293
-		}
294
-		foreach ($this->backends as $backend) {
295
-			if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) {
296
-				$result = true;
297
-				$backend->deleteGroup($this->gid);
298
-			}
299
-		}
300
-		if ($result and $this->emitter) {
301
-			$this->emitter->emit('\OC\Group', 'postDelete', array($this));
302
-		}
303
-		return $result;
304
-	}
290
+        $result = false;
291
+        if ($this->emitter) {
292
+            $this->emitter->emit('\OC\Group', 'preDelete', array($this));
293
+        }
294
+        foreach ($this->backends as $backend) {
295
+            if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) {
296
+                $result = true;
297
+                $backend->deleteGroup($this->gid);
298
+            }
299
+        }
300
+        if ($result and $this->emitter) {
301
+            $this->emitter->emit('\OC\Group', 'postDelete', array($this));
302
+        }
303
+        return $result;
304
+    }
305 305
 
306
-	/**
307
-	 * returns all the Users from an array that really exists
308
-	 * @param string[] $userIds an array containing user IDs
309
-	 * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value
310
-	 */
311
-	private function getVerifiedUsers($userIds) {
312
-		if (!is_array($userIds)) {
313
-			return array();
314
-		}
315
-		$users = array();
316
-		foreach ($userIds as $userId) {
317
-			$user = $this->userManager->get($userId);
318
-			if (!is_null($user)) {
319
-				$users[$userId] = $user;
320
-			}
321
-		}
322
-		return $users;
323
-	}
306
+    /**
307
+     * returns all the Users from an array that really exists
308
+     * @param string[] $userIds an array containing user IDs
309
+     * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value
310
+     */
311
+    private function getVerifiedUsers($userIds) {
312
+        if (!is_array($userIds)) {
313
+            return array();
314
+        }
315
+        $users = array();
316
+        foreach ($userIds as $userId) {
317
+            $user = $this->userManager->get($userId);
318
+            if (!is_null($user)) {
319
+                $users[$userId] = $user;
320
+            }
321
+        }
322
+        return $users;
323
+    }
324 324
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -224,8 +224,8 @@  discard block
 block discarded – undo
224 224
 	public function count($search = '') {
225 225
 		$users = false;
226 226
 		foreach ($this->backends as $backend) {
227
-			if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) {
228
-				if($users === false) {
227
+			if ($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) {
228
+				if ($users === false) {
229 229
 					//we could directly add to a bool variable, but this would
230 230
 					//be ugly
231 231
 					$users = 0;
@@ -244,8 +244,8 @@  discard block
 block discarded – undo
244 244
 	public function countDisabled() {
245 245
 		$users = false;
246 246
 		foreach ($this->backends as $backend) {
247
-			if($backend->implementsActions(\OC\Group\Backend::COUNT_DISABLED)) {
248
-				if($users === false) {
247
+			if ($backend->implementsActions(\OC\Group\Backend::COUNT_DISABLED)) {
248
+				if ($users === false) {
249 249
 					//we could directly add to a bool variable, but this would
250 250
 					//be ugly
251 251
 					$users = 0;
Please login to merge, or discard this patch.
lib/private/User/Manager.php 1 patch
Indentation   +486 added lines, -486 removed lines patch added patch discarded remove patch
@@ -57,490 +57,490 @@
 block discarded – undo
57 57
  * @package OC\User
58 58
  */
59 59
 class Manager extends PublicEmitter implements IUserManager {
60
-	/**
61
-	 * @var \OCP\UserInterface[] $backends
62
-	 */
63
-	private $backends = array();
64
-
65
-	/**
66
-	 * @var \OC\User\User[] $cachedUsers
67
-	 */
68
-	private $cachedUsers = array();
69
-
70
-	/**
71
-	 * @var \OCP\IConfig $config
72
-	 */
73
-	private $config;
74
-
75
-	/**
76
-	 * @param \OCP\IConfig $config
77
-	 */
78
-	public function __construct(IConfig $config) {
79
-		$this->config = $config;
80
-		$cachedUsers = &$this->cachedUsers;
81
-		$this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) {
82
-			/** @var \OC\User\User $user */
83
-			unset($cachedUsers[$user->getUID()]);
84
-		});
85
-	}
86
-
87
-	/**
88
-	 * Get the active backends
89
-	 * @return \OCP\UserInterface[]
90
-	 */
91
-	public function getBackends() {
92
-		return $this->backends;
93
-	}
94
-
95
-	/**
96
-	 * register a user backend
97
-	 *
98
-	 * @param \OCP\UserInterface $backend
99
-	 */
100
-	public function registerBackend($backend) {
101
-		$this->backends[] = $backend;
102
-	}
103
-
104
-	/**
105
-	 * remove a user backend
106
-	 *
107
-	 * @param \OCP\UserInterface $backend
108
-	 */
109
-	public function removeBackend($backend) {
110
-		$this->cachedUsers = array();
111
-		if (($i = array_search($backend, $this->backends)) !== false) {
112
-			unset($this->backends[$i]);
113
-		}
114
-	}
115
-
116
-	/**
117
-	 * remove all user backends
118
-	 */
119
-	public function clearBackends() {
120
-		$this->cachedUsers = array();
121
-		$this->backends = array();
122
-	}
123
-
124
-	/**
125
-	 * get a user by user id
126
-	 *
127
-	 * @param string $uid
128
-	 * @return \OC\User\User|null Either the user or null if the specified user does not exist
129
-	 */
130
-	public function get($uid) {
131
-		if (is_null($uid) || $uid === '' || $uid === false) {
132
-			return null;
133
-		}
134
-		if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends
135
-			return $this->cachedUsers[$uid];
136
-		}
137
-		foreach ($this->backends as $backend) {
138
-			if ($backend->userExists($uid)) {
139
-				return $this->getUserObject($uid, $backend);
140
-			}
141
-		}
142
-		return null;
143
-	}
144
-
145
-	/**
146
-	 * get or construct the user object
147
-	 *
148
-	 * @param string $uid
149
-	 * @param \OCP\UserInterface $backend
150
-	 * @param bool $cacheUser If false the newly created user object will not be cached
151
-	 * @return \OC\User\User
152
-	 */
153
-	protected function getUserObject($uid, $backend, $cacheUser = true) {
154
-		if (isset($this->cachedUsers[$uid])) {
155
-			return $this->cachedUsers[$uid];
156
-		}
157
-
158
-		$user = new User($uid, $backend, $this, $this->config);
159
-		if ($cacheUser) {
160
-			$this->cachedUsers[$uid] = $user;
161
-		}
162
-		return $user;
163
-	}
164
-
165
-	/**
166
-	 * check if a user exists
167
-	 *
168
-	 * @param string $uid
169
-	 * @return bool
170
-	 */
171
-	public function userExists($uid) {
172
-		$user = $this->get($uid);
173
-		return ($user !== null);
174
-	}
175
-
176
-	/**
177
-	 * Check if the password is valid for the user
178
-	 *
179
-	 * @param string $loginName
180
-	 * @param string $password
181
-	 * @return mixed the User object on success, false otherwise
182
-	 */
183
-	public function checkPassword($loginName, $password) {
184
-		$result = $this->checkPasswordNoLogging($loginName, $password);
185
-
186
-		if ($result === false) {
187
-			\OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']);
188
-		}
189
-
190
-		return $result;
191
-	}
192
-
193
-	/**
194
-	 * Check if the password is valid for the user
195
-	 *
196
-	 * @internal
197
-	 * @param string $loginName
198
-	 * @param string $password
199
-	 * @return mixed the User object on success, false otherwise
200
-	 */
201
-	public function checkPasswordNoLogging($loginName, $password) {
202
-		$loginName = str_replace("\0", '', $loginName);
203
-		$password = str_replace("\0", '', $password);
204
-
205
-		foreach ($this->backends as $backend) {
206
-			if ($backend->implementsActions(Backend::CHECK_PASSWORD)) {
207
-				$uid = $backend->checkPassword($loginName, $password);
208
-				if ($uid !== false) {
209
-					return $this->getUserObject($uid, $backend);
210
-				}
211
-			}
212
-		}
213
-
214
-		return false;
215
-	}
216
-
217
-	/**
218
-	 * search by user id
219
-	 *
220
-	 * @param string $pattern
221
-	 * @param int $limit
222
-	 * @param int $offset
223
-	 * @return \OC\User\User[]
224
-	 */
225
-	public function search($pattern, $limit = null, $offset = null) {
226
-		$users = array();
227
-		foreach ($this->backends as $backend) {
228
-			$backendUsers = $backend->getUsers($pattern, $limit, $offset);
229
-			if (is_array($backendUsers)) {
230
-				foreach ($backendUsers as $uid) {
231
-					$users[$uid] = $this->getUserObject($uid, $backend);
232
-				}
233
-			}
234
-		}
235
-
236
-		uasort($users, function ($a, $b) {
237
-			/**
238
-			 * @var \OC\User\User $a
239
-			 * @var \OC\User\User $b
240
-			 */
241
-			return strcmp($a->getUID(), $b->getUID());
242
-		});
243
-		return $users;
244
-	}
245
-
246
-	/**
247
-	 * search by displayName
248
-	 *
249
-	 * @param string $pattern
250
-	 * @param int $limit
251
-	 * @param int $offset
252
-	 * @return \OC\User\User[]
253
-	 */
254
-	public function searchDisplayName($pattern, $limit = null, $offset = null) {
255
-		$users = array();
256
-		foreach ($this->backends as $backend) {
257
-			$backendUsers = $backend->getDisplayNames($pattern, $limit, $offset);
258
-			if (is_array($backendUsers)) {
259
-				foreach ($backendUsers as $uid => $displayName) {
260
-					$users[] = $this->getUserObject($uid, $backend);
261
-				}
262
-			}
263
-		}
264
-
265
-		usort($users, function ($a, $b) {
266
-			/**
267
-			 * @var \OC\User\User $a
268
-			 * @var \OC\User\User $b
269
-			 */
270
-			return strcmp(strtolower($a->getDisplayName()), strtolower($b->getDisplayName()));
271
-		});
272
-		return $users;
273
-	}
274
-
275
-	/**
276
-	 * @param string $uid
277
-	 * @param string $password
278
-	 * @throws \InvalidArgumentException
279
-	 * @return bool|IUser the created user or false
280
-	 */
281
-	public function createUser($uid, $password) {
282
-		$localBackends = [];
283
-		foreach ($this->backends as $backend) {
284
-			if ($backend instanceof Database) {
285
-				// First check if there is another user backend
286
-				$localBackends[] = $backend;
287
-				continue;
288
-			}
289
-
290
-			if ($backend->implementsActions(Backend::CREATE_USER)) {
291
-				return $this->createUserFromBackend($uid, $password, $backend);
292
-			}
293
-		}
294
-
295
-		foreach ($localBackends as $backend) {
296
-			if ($backend->implementsActions(Backend::CREATE_USER)) {
297
-				return $this->createUserFromBackend($uid, $password, $backend);
298
-			}
299
-		}
300
-
301
-		return false;
302
-	}
303
-
304
-	/**
305
-	 * @param string $uid
306
-	 * @param string $password
307
-	 * @param UserInterface $backend
308
-	 * @return IUser|null
309
-	 * @throws \InvalidArgumentException
310
-	 */
311
-	public function createUserFromBackend($uid, $password, UserInterface $backend) {
312
-		$l = \OC::$server->getL10N('lib');
313
-
314
-		// Check the name for bad characters
315
-		// Allowed are: "a-z", "A-Z", "0-9" and "_.@-'"
316
-		if (preg_match('/[^a-zA-Z0-9 _\.@\-\']/', $uid)) {
317
-			throw new \InvalidArgumentException($l->t('Only the following characters are allowed in a username:'
318
-				. ' "a-z", "A-Z", "0-9", and "_.@-\'"'));
319
-		}
320
-		// No empty username
321
-		if (trim($uid) === '') {
322
-			throw new \InvalidArgumentException($l->t('A valid username must be provided'));
323
-		}
324
-		// No whitespace at the beginning or at the end
325
-		if (trim($uid) !== $uid) {
326
-			throw new \InvalidArgumentException($l->t('Username contains whitespace at the beginning or at the end'));
327
-		}
328
-		// Username only consists of 1 or 2 dots (directory traversal)
329
-		if ($uid === '.' || $uid === '..') {
330
-			throw new \InvalidArgumentException($l->t('Username must not consist of dots only'));
331
-		}
332
-		// No empty password
333
-		if (trim($password) === '') {
334
-			throw new \InvalidArgumentException($l->t('A valid password must be provided'));
335
-		}
336
-
337
-		// Check if user already exists
338
-		if ($this->userExists($uid)) {
339
-			throw new \InvalidArgumentException($l->t('The username is already being used'));
340
-		}
341
-
342
-		$this->emit('\OC\User', 'preCreateUser', [$uid, $password]);
343
-		$state = $backend->createUser($uid, $password);
344
-		if($state === false) {
345
-			throw new \InvalidArgumentException($l->t('Could not create user'));
346
-		}
347
-		$user = $this->getUserObject($uid, $backend);
348
-		if ($user instanceof IUser) {
349
-			$this->emit('\OC\User', 'postCreateUser', [$user, $password]);
350
-		}
351
-		return $user;
352
-	}
353
-
354
-	/**
355
-	 * returns how many users per backend exist (if supported by backend)
356
-	 *
357
-	 * @param boolean $hasLoggedIn when true only users that have a lastLogin
358
-	 *                entry in the preferences table will be affected
359
-	 * @return array|int an array of backend class as key and count number as value
360
-	 *                if $hasLoggedIn is true only an int is returned
361
-	 */
362
-	public function countUsers($hasLoggedIn = false) {
363
-		if ($hasLoggedIn) {
364
-			return $this->countSeenUsers();
365
-		}
366
-		$userCountStatistics = [];
367
-		foreach ($this->backends as $backend) {
368
-			if ($backend->implementsActions(Backend::COUNT_USERS)) {
369
-				$backendUsers = $backend->countUsers();
370
-				if($backendUsers !== false) {
371
-					if($backend instanceof IUserBackend) {
372
-						$name = $backend->getBackendName();
373
-					} else {
374
-						$name = get_class($backend);
375
-					}
376
-					if(isset($userCountStatistics[$name])) {
377
-						$userCountStatistics[$name] += $backendUsers;
378
-					} else {
379
-						$userCountStatistics[$name] = $backendUsers;
380
-					}
381
-				}
382
-			}
383
-		}
384
-		return $userCountStatistics;
385
-	}
386
-
387
-	/**
388
-	 * The callback is executed for each user on each backend.
389
-	 * If the callback returns false no further users will be retrieved.
390
-	 *
391
-	 * @param \Closure $callback
392
-	 * @param string $search
393
-	 * @param boolean $onlySeen when true only users that have a lastLogin entry
394
-	 *                in the preferences table will be affected
395
-	 * @since 9.0.0
396
-	 */
397
-	public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) {
398
-		if ($onlySeen) {
399
-			$this->callForSeenUsers($callback);
400
-		} else {
401
-			foreach ($this->getBackends() as $backend) {
402
-				$limit = 500;
403
-				$offset = 0;
404
-				do {
405
-					$users = $backend->getUsers($search, $limit, $offset);
406
-					foreach ($users as $uid) {
407
-						if (!$backend->userExists($uid)) {
408
-							continue;
409
-						}
410
-						$user = $this->getUserObject($uid, $backend, false);
411
-						$return = $callback($user);
412
-						if ($return === false) {
413
-							break;
414
-						}
415
-					}
416
-					$offset += $limit;
417
-				} while (count($users) >= $limit);
418
-			}
419
-		}
420
-	}
421
-
422
-	/**
423
-	 * returns how many users are disabled in the requested groups
424
-	 *
425
-	 * @param array $groups groupids to search
426
-	 * @return int
427
-	 * @since 14.0.0
428
-	 */
429
-	public function countDisabledUsersOfGroups(array $groups):int {
430
-		$queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
431
-		$queryBuilder->select($queryBuilder->createFunction('COUNT(Distinct uid)'))
432
-			->from('preferences', 'p')
433
-			->innerJoin('p', 'group_user', 'g', 'p.userid = g.uid')
434
-			->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core')))
435
-			->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled')))
436
-			->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR))
437
-			->andWhere($queryBuilder->expr()->in('gid', $queryBuilder->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY)));
438
-
439
-		$query = $queryBuilder->execute();
440
-
441
-		$result = (int)$query->fetchColumn();
442
-		$query->closeCursor();
443
-
444
-		return $result;
445
-	}
446
-
447
-	/**
448
-	 * returns how many users have logged in once
449
-	 *
450
-	 * @return int
451
-	 * @since 11.0.0
452
-	 */
453
-	public function countSeenUsers() {
454
-		$queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
455
-		$queryBuilder->select($queryBuilder->createFunction('COUNT(*)'))
456
-			->from('preferences')
457
-			->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login')))
458
-			->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('lastLogin')))
459
-			->andWhere($queryBuilder->expr()->isNotNull('configvalue'));
460
-
461
-		$query = $queryBuilder->execute();
462
-
463
-		$result = (int)$query->fetchColumn();
464
-		$query->closeCursor();
465
-
466
-		return $result;
467
-	}
468
-
469
-	/**
470
-	 * @param \Closure $callback
471
-	 * @since 11.0.0
472
-	 */
473
-	public function callForSeenUsers(\Closure $callback) {
474
-		$limit = 1000;
475
-		$offset = 0;
476
-		do {
477
-			$userIds = $this->getSeenUserIds($limit, $offset);
478
-			$offset += $limit;
479
-			foreach ($userIds as $userId) {
480
-				foreach ($this->backends as $backend) {
481
-					if ($backend->userExists($userId)) {
482
-						$user = $this->getUserObject($userId, $backend, false);
483
-						$return = $callback($user);
484
-						if ($return === false) {
485
-							return;
486
-						}
487
-						break;
488
-					}
489
-				}
490
-			}
491
-		} while (count($userIds) >= $limit);
492
-	}
493
-
494
-	/**
495
-	 * Getting all userIds that have a listLogin value requires checking the
496
-	 * value in php because on oracle you cannot use a clob in a where clause,
497
-	 * preventing us from doing a not null or length(value) > 0 check.
498
-	 *
499
-	 * @param int $limit
500
-	 * @param int $offset
501
-	 * @return string[] with user ids
502
-	 */
503
-	private function getSeenUserIds($limit = null, $offset = null) {
504
-		$queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
505
-		$queryBuilder->select(['userid'])
506
-			->from('preferences')
507
-			->where($queryBuilder->expr()->eq(
508
-				'appid', $queryBuilder->createNamedParameter('login'))
509
-			)
510
-			->andWhere($queryBuilder->expr()->eq(
511
-				'configkey', $queryBuilder->createNamedParameter('lastLogin'))
512
-			)
513
-			->andWhere($queryBuilder->expr()->isNotNull('configvalue')
514
-			);
515
-
516
-		if ($limit !== null) {
517
-			$queryBuilder->setMaxResults($limit);
518
-		}
519
-		if ($offset !== null) {
520
-			$queryBuilder->setFirstResult($offset);
521
-		}
522
-		$query = $queryBuilder->execute();
523
-		$result = [];
524
-
525
-		while ($row = $query->fetch()) {
526
-			$result[] = $row['userid'];
527
-		}
528
-
529
-		$query->closeCursor();
530
-
531
-		return $result;
532
-	}
533
-
534
-	/**
535
-	 * @param string $email
536
-	 * @return IUser[]
537
-	 * @since 9.1.0
538
-	 */
539
-	public function getByEmail($email) {
540
-		$userIds = $this->config->getUsersForUserValue('settings', 'email', $email);
541
-
542
-		return array_map(function($uid) {
543
-			return $this->get($uid);
544
-		}, $userIds);
545
-	}
60
+    /**
61
+     * @var \OCP\UserInterface[] $backends
62
+     */
63
+    private $backends = array();
64
+
65
+    /**
66
+     * @var \OC\User\User[] $cachedUsers
67
+     */
68
+    private $cachedUsers = array();
69
+
70
+    /**
71
+     * @var \OCP\IConfig $config
72
+     */
73
+    private $config;
74
+
75
+    /**
76
+     * @param \OCP\IConfig $config
77
+     */
78
+    public function __construct(IConfig $config) {
79
+        $this->config = $config;
80
+        $cachedUsers = &$this->cachedUsers;
81
+        $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) {
82
+            /** @var \OC\User\User $user */
83
+            unset($cachedUsers[$user->getUID()]);
84
+        });
85
+    }
86
+
87
+    /**
88
+     * Get the active backends
89
+     * @return \OCP\UserInterface[]
90
+     */
91
+    public function getBackends() {
92
+        return $this->backends;
93
+    }
94
+
95
+    /**
96
+     * register a user backend
97
+     *
98
+     * @param \OCP\UserInterface $backend
99
+     */
100
+    public function registerBackend($backend) {
101
+        $this->backends[] = $backend;
102
+    }
103
+
104
+    /**
105
+     * remove a user backend
106
+     *
107
+     * @param \OCP\UserInterface $backend
108
+     */
109
+    public function removeBackend($backend) {
110
+        $this->cachedUsers = array();
111
+        if (($i = array_search($backend, $this->backends)) !== false) {
112
+            unset($this->backends[$i]);
113
+        }
114
+    }
115
+
116
+    /**
117
+     * remove all user backends
118
+     */
119
+    public function clearBackends() {
120
+        $this->cachedUsers = array();
121
+        $this->backends = array();
122
+    }
123
+
124
+    /**
125
+     * get a user by user id
126
+     *
127
+     * @param string $uid
128
+     * @return \OC\User\User|null Either the user or null if the specified user does not exist
129
+     */
130
+    public function get($uid) {
131
+        if (is_null($uid) || $uid === '' || $uid === false) {
132
+            return null;
133
+        }
134
+        if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends
135
+            return $this->cachedUsers[$uid];
136
+        }
137
+        foreach ($this->backends as $backend) {
138
+            if ($backend->userExists($uid)) {
139
+                return $this->getUserObject($uid, $backend);
140
+            }
141
+        }
142
+        return null;
143
+    }
144
+
145
+    /**
146
+     * get or construct the user object
147
+     *
148
+     * @param string $uid
149
+     * @param \OCP\UserInterface $backend
150
+     * @param bool $cacheUser If false the newly created user object will not be cached
151
+     * @return \OC\User\User
152
+     */
153
+    protected function getUserObject($uid, $backend, $cacheUser = true) {
154
+        if (isset($this->cachedUsers[$uid])) {
155
+            return $this->cachedUsers[$uid];
156
+        }
157
+
158
+        $user = new User($uid, $backend, $this, $this->config);
159
+        if ($cacheUser) {
160
+            $this->cachedUsers[$uid] = $user;
161
+        }
162
+        return $user;
163
+    }
164
+
165
+    /**
166
+     * check if a user exists
167
+     *
168
+     * @param string $uid
169
+     * @return bool
170
+     */
171
+    public function userExists($uid) {
172
+        $user = $this->get($uid);
173
+        return ($user !== null);
174
+    }
175
+
176
+    /**
177
+     * Check if the password is valid for the user
178
+     *
179
+     * @param string $loginName
180
+     * @param string $password
181
+     * @return mixed the User object on success, false otherwise
182
+     */
183
+    public function checkPassword($loginName, $password) {
184
+        $result = $this->checkPasswordNoLogging($loginName, $password);
185
+
186
+        if ($result === false) {
187
+            \OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']);
188
+        }
189
+
190
+        return $result;
191
+    }
192
+
193
+    /**
194
+     * Check if the password is valid for the user
195
+     *
196
+     * @internal
197
+     * @param string $loginName
198
+     * @param string $password
199
+     * @return mixed the User object on success, false otherwise
200
+     */
201
+    public function checkPasswordNoLogging($loginName, $password) {
202
+        $loginName = str_replace("\0", '', $loginName);
203
+        $password = str_replace("\0", '', $password);
204
+
205
+        foreach ($this->backends as $backend) {
206
+            if ($backend->implementsActions(Backend::CHECK_PASSWORD)) {
207
+                $uid = $backend->checkPassword($loginName, $password);
208
+                if ($uid !== false) {
209
+                    return $this->getUserObject($uid, $backend);
210
+                }
211
+            }
212
+        }
213
+
214
+        return false;
215
+    }
216
+
217
+    /**
218
+     * search by user id
219
+     *
220
+     * @param string $pattern
221
+     * @param int $limit
222
+     * @param int $offset
223
+     * @return \OC\User\User[]
224
+     */
225
+    public function search($pattern, $limit = null, $offset = null) {
226
+        $users = array();
227
+        foreach ($this->backends as $backend) {
228
+            $backendUsers = $backend->getUsers($pattern, $limit, $offset);
229
+            if (is_array($backendUsers)) {
230
+                foreach ($backendUsers as $uid) {
231
+                    $users[$uid] = $this->getUserObject($uid, $backend);
232
+                }
233
+            }
234
+        }
235
+
236
+        uasort($users, function ($a, $b) {
237
+            /**
238
+             * @var \OC\User\User $a
239
+             * @var \OC\User\User $b
240
+             */
241
+            return strcmp($a->getUID(), $b->getUID());
242
+        });
243
+        return $users;
244
+    }
245
+
246
+    /**
247
+     * search by displayName
248
+     *
249
+     * @param string $pattern
250
+     * @param int $limit
251
+     * @param int $offset
252
+     * @return \OC\User\User[]
253
+     */
254
+    public function searchDisplayName($pattern, $limit = null, $offset = null) {
255
+        $users = array();
256
+        foreach ($this->backends as $backend) {
257
+            $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset);
258
+            if (is_array($backendUsers)) {
259
+                foreach ($backendUsers as $uid => $displayName) {
260
+                    $users[] = $this->getUserObject($uid, $backend);
261
+                }
262
+            }
263
+        }
264
+
265
+        usort($users, function ($a, $b) {
266
+            /**
267
+             * @var \OC\User\User $a
268
+             * @var \OC\User\User $b
269
+             */
270
+            return strcmp(strtolower($a->getDisplayName()), strtolower($b->getDisplayName()));
271
+        });
272
+        return $users;
273
+    }
274
+
275
+    /**
276
+     * @param string $uid
277
+     * @param string $password
278
+     * @throws \InvalidArgumentException
279
+     * @return bool|IUser the created user or false
280
+     */
281
+    public function createUser($uid, $password) {
282
+        $localBackends = [];
283
+        foreach ($this->backends as $backend) {
284
+            if ($backend instanceof Database) {
285
+                // First check if there is another user backend
286
+                $localBackends[] = $backend;
287
+                continue;
288
+            }
289
+
290
+            if ($backend->implementsActions(Backend::CREATE_USER)) {
291
+                return $this->createUserFromBackend($uid, $password, $backend);
292
+            }
293
+        }
294
+
295
+        foreach ($localBackends as $backend) {
296
+            if ($backend->implementsActions(Backend::CREATE_USER)) {
297
+                return $this->createUserFromBackend($uid, $password, $backend);
298
+            }
299
+        }
300
+
301
+        return false;
302
+    }
303
+
304
+    /**
305
+     * @param string $uid
306
+     * @param string $password
307
+     * @param UserInterface $backend
308
+     * @return IUser|null
309
+     * @throws \InvalidArgumentException
310
+     */
311
+    public function createUserFromBackend($uid, $password, UserInterface $backend) {
312
+        $l = \OC::$server->getL10N('lib');
313
+
314
+        // Check the name for bad characters
315
+        // Allowed are: "a-z", "A-Z", "0-9" and "_.@-'"
316
+        if (preg_match('/[^a-zA-Z0-9 _\.@\-\']/', $uid)) {
317
+            throw new \InvalidArgumentException($l->t('Only the following characters are allowed in a username:'
318
+                . ' "a-z", "A-Z", "0-9", and "_.@-\'"'));
319
+        }
320
+        // No empty username
321
+        if (trim($uid) === '') {
322
+            throw new \InvalidArgumentException($l->t('A valid username must be provided'));
323
+        }
324
+        // No whitespace at the beginning or at the end
325
+        if (trim($uid) !== $uid) {
326
+            throw new \InvalidArgumentException($l->t('Username contains whitespace at the beginning or at the end'));
327
+        }
328
+        // Username only consists of 1 or 2 dots (directory traversal)
329
+        if ($uid === '.' || $uid === '..') {
330
+            throw new \InvalidArgumentException($l->t('Username must not consist of dots only'));
331
+        }
332
+        // No empty password
333
+        if (trim($password) === '') {
334
+            throw new \InvalidArgumentException($l->t('A valid password must be provided'));
335
+        }
336
+
337
+        // Check if user already exists
338
+        if ($this->userExists($uid)) {
339
+            throw new \InvalidArgumentException($l->t('The username is already being used'));
340
+        }
341
+
342
+        $this->emit('\OC\User', 'preCreateUser', [$uid, $password]);
343
+        $state = $backend->createUser($uid, $password);
344
+        if($state === false) {
345
+            throw new \InvalidArgumentException($l->t('Could not create user'));
346
+        }
347
+        $user = $this->getUserObject($uid, $backend);
348
+        if ($user instanceof IUser) {
349
+            $this->emit('\OC\User', 'postCreateUser', [$user, $password]);
350
+        }
351
+        return $user;
352
+    }
353
+
354
+    /**
355
+     * returns how many users per backend exist (if supported by backend)
356
+     *
357
+     * @param boolean $hasLoggedIn when true only users that have a lastLogin
358
+     *                entry in the preferences table will be affected
359
+     * @return array|int an array of backend class as key and count number as value
360
+     *                if $hasLoggedIn is true only an int is returned
361
+     */
362
+    public function countUsers($hasLoggedIn = false) {
363
+        if ($hasLoggedIn) {
364
+            return $this->countSeenUsers();
365
+        }
366
+        $userCountStatistics = [];
367
+        foreach ($this->backends as $backend) {
368
+            if ($backend->implementsActions(Backend::COUNT_USERS)) {
369
+                $backendUsers = $backend->countUsers();
370
+                if($backendUsers !== false) {
371
+                    if($backend instanceof IUserBackend) {
372
+                        $name = $backend->getBackendName();
373
+                    } else {
374
+                        $name = get_class($backend);
375
+                    }
376
+                    if(isset($userCountStatistics[$name])) {
377
+                        $userCountStatistics[$name] += $backendUsers;
378
+                    } else {
379
+                        $userCountStatistics[$name] = $backendUsers;
380
+                    }
381
+                }
382
+            }
383
+        }
384
+        return $userCountStatistics;
385
+    }
386
+
387
+    /**
388
+     * The callback is executed for each user on each backend.
389
+     * If the callback returns false no further users will be retrieved.
390
+     *
391
+     * @param \Closure $callback
392
+     * @param string $search
393
+     * @param boolean $onlySeen when true only users that have a lastLogin entry
394
+     *                in the preferences table will be affected
395
+     * @since 9.0.0
396
+     */
397
+    public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) {
398
+        if ($onlySeen) {
399
+            $this->callForSeenUsers($callback);
400
+        } else {
401
+            foreach ($this->getBackends() as $backend) {
402
+                $limit = 500;
403
+                $offset = 0;
404
+                do {
405
+                    $users = $backend->getUsers($search, $limit, $offset);
406
+                    foreach ($users as $uid) {
407
+                        if (!$backend->userExists($uid)) {
408
+                            continue;
409
+                        }
410
+                        $user = $this->getUserObject($uid, $backend, false);
411
+                        $return = $callback($user);
412
+                        if ($return === false) {
413
+                            break;
414
+                        }
415
+                    }
416
+                    $offset += $limit;
417
+                } while (count($users) >= $limit);
418
+            }
419
+        }
420
+    }
421
+
422
+    /**
423
+     * returns how many users are disabled in the requested groups
424
+     *
425
+     * @param array $groups groupids to search
426
+     * @return int
427
+     * @since 14.0.0
428
+     */
429
+    public function countDisabledUsersOfGroups(array $groups):int {
430
+        $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
431
+        $queryBuilder->select($queryBuilder->createFunction('COUNT(Distinct uid)'))
432
+            ->from('preferences', 'p')
433
+            ->innerJoin('p', 'group_user', 'g', 'p.userid = g.uid')
434
+            ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core')))
435
+            ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled')))
436
+            ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR))
437
+            ->andWhere($queryBuilder->expr()->in('gid', $queryBuilder->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY)));
438
+
439
+        $query = $queryBuilder->execute();
440
+
441
+        $result = (int)$query->fetchColumn();
442
+        $query->closeCursor();
443
+
444
+        return $result;
445
+    }
446
+
447
+    /**
448
+     * returns how many users have logged in once
449
+     *
450
+     * @return int
451
+     * @since 11.0.0
452
+     */
453
+    public function countSeenUsers() {
454
+        $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
455
+        $queryBuilder->select($queryBuilder->createFunction('COUNT(*)'))
456
+            ->from('preferences')
457
+            ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login')))
458
+            ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('lastLogin')))
459
+            ->andWhere($queryBuilder->expr()->isNotNull('configvalue'));
460
+
461
+        $query = $queryBuilder->execute();
462
+
463
+        $result = (int)$query->fetchColumn();
464
+        $query->closeCursor();
465
+
466
+        return $result;
467
+    }
468
+
469
+    /**
470
+     * @param \Closure $callback
471
+     * @since 11.0.0
472
+     */
473
+    public function callForSeenUsers(\Closure $callback) {
474
+        $limit = 1000;
475
+        $offset = 0;
476
+        do {
477
+            $userIds = $this->getSeenUserIds($limit, $offset);
478
+            $offset += $limit;
479
+            foreach ($userIds as $userId) {
480
+                foreach ($this->backends as $backend) {
481
+                    if ($backend->userExists($userId)) {
482
+                        $user = $this->getUserObject($userId, $backend, false);
483
+                        $return = $callback($user);
484
+                        if ($return === false) {
485
+                            return;
486
+                        }
487
+                        break;
488
+                    }
489
+                }
490
+            }
491
+        } while (count($userIds) >= $limit);
492
+    }
493
+
494
+    /**
495
+     * Getting all userIds that have a listLogin value requires checking the
496
+     * value in php because on oracle you cannot use a clob in a where clause,
497
+     * preventing us from doing a not null or length(value) > 0 check.
498
+     *
499
+     * @param int $limit
500
+     * @param int $offset
501
+     * @return string[] with user ids
502
+     */
503
+    private function getSeenUserIds($limit = null, $offset = null) {
504
+        $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
505
+        $queryBuilder->select(['userid'])
506
+            ->from('preferences')
507
+            ->where($queryBuilder->expr()->eq(
508
+                'appid', $queryBuilder->createNamedParameter('login'))
509
+            )
510
+            ->andWhere($queryBuilder->expr()->eq(
511
+                'configkey', $queryBuilder->createNamedParameter('lastLogin'))
512
+            )
513
+            ->andWhere($queryBuilder->expr()->isNotNull('configvalue')
514
+            );
515
+
516
+        if ($limit !== null) {
517
+            $queryBuilder->setMaxResults($limit);
518
+        }
519
+        if ($offset !== null) {
520
+            $queryBuilder->setFirstResult($offset);
521
+        }
522
+        $query = $queryBuilder->execute();
523
+        $result = [];
524
+
525
+        while ($row = $query->fetch()) {
526
+            $result[] = $row['userid'];
527
+        }
528
+
529
+        $query->closeCursor();
530
+
531
+        return $result;
532
+    }
533
+
534
+    /**
535
+     * @param string $email
536
+     * @return IUser[]
537
+     * @since 9.1.0
538
+     */
539
+    public function getByEmail($email) {
540
+        $userIds = $this->config->getUsersForUserValue('settings', 'email', $email);
541
+
542
+        return array_map(function($uid) {
543
+            return $this->get($uid);
544
+        }, $userIds);
545
+    }
546 546
 }
Please login to merge, or discard this patch.
lib/public/IGroup.php 1 patch
Indentation   +82 added lines, -82 removed lines patch added patch discarded remove patch
@@ -33,96 +33,96 @@
 block discarded – undo
33 33
  * @since 8.0.0
34 34
  */
35 35
 interface IGroup {
36
-	/**
37
-	 * @return string
38
-	 * @since 8.0.0
39
-	 */
40
-	public function getGID();
36
+    /**
37
+     * @return string
38
+     * @since 8.0.0
39
+     */
40
+    public function getGID();
41 41
 
42
-	/**
43
-	 * Returns the group display name
44
-	 *
45
-	 * @return string
46
-	 * @since 12.0.0
47
-	 */
48
-	public function getDisplayName();
42
+    /**
43
+     * Returns the group display name
44
+     *
45
+     * @return string
46
+     * @since 12.0.0
47
+     */
48
+    public function getDisplayName();
49 49
 
50
-	/**
51
-	 * get all users in the group
52
-	 *
53
-	 * @return \OCP\IUser[]
54
-	 * @since 8.0.0
55
-	 */
56
-	public function getUsers();
50
+    /**
51
+     * get all users in the group
52
+     *
53
+     * @return \OCP\IUser[]
54
+     * @since 8.0.0
55
+     */
56
+    public function getUsers();
57 57
 
58
-	/**
59
-	 * check if a user is in the group
60
-	 *
61
-	 * @param \OCP\IUser $user
62
-	 * @return bool
63
-	 * @since 8.0.0
64
-	 */
65
-	public function inGroup(IUser $user);
58
+    /**
59
+     * check if a user is in the group
60
+     *
61
+     * @param \OCP\IUser $user
62
+     * @return bool
63
+     * @since 8.0.0
64
+     */
65
+    public function inGroup(IUser $user);
66 66
 
67
-	/**
68
-	 * add a user to the group
69
-	 *
70
-	 * @param \OCP\IUser $user
71
-	 * @since 8.0.0
72
-	 */
73
-	public function addUser(IUser $user);
67
+    /**
68
+     * add a user to the group
69
+     *
70
+     * @param \OCP\IUser $user
71
+     * @since 8.0.0
72
+     */
73
+    public function addUser(IUser $user);
74 74
 
75
-	/**
76
-	 * remove a user from the group
77
-	 *
78
-	 * @param \OCP\IUser $user
79
-	 * @since 8.0.0
80
-	 */
81
-	public function removeUser($user);
75
+    /**
76
+     * remove a user from the group
77
+     *
78
+     * @param \OCP\IUser $user
79
+     * @since 8.0.0
80
+     */
81
+    public function removeUser($user);
82 82
 
83
-	/**
84
-	 * search for users in the group by userid
85
-	 *
86
-	 * @param string $search
87
-	 * @param int $limit
88
-	 * @param int $offset
89
-	 * @return \OCP\IUser[]
90
-	 * @since 8.0.0
91
-	 */
92
-	public function searchUsers($search, $limit = null, $offset = null);
83
+    /**
84
+     * search for users in the group by userid
85
+     *
86
+     * @param string $search
87
+     * @param int $limit
88
+     * @param int $offset
89
+     * @return \OCP\IUser[]
90
+     * @since 8.0.0
91
+     */
92
+    public function searchUsers($search, $limit = null, $offset = null);
93 93
 
94
-	/**
95
-	 * returns the number of users matching the search string
96
-	 *
97
-	 * @param string $search
98
-	 * @return int|bool
99
-	 * @since 8.0.0
100
-	 */
101
-	public function count($search = '');
94
+    /**
95
+     * returns the number of users matching the search string
96
+     *
97
+     * @param string $search
98
+     * @return int|bool
99
+     * @since 8.0.0
100
+     */
101
+    public function count($search = '');
102 102
 
103
-	/**
104
-	 * returns the number of disabled users
105
-	 *
106
-	 * @since 14.0.0
107
-	 */
108
-	public function countDisabled();
103
+    /**
104
+     * returns the number of disabled users
105
+     *
106
+     * @since 14.0.0
107
+     */
108
+    public function countDisabled();
109 109
 
110
-	/**
111
-	 * search for users in the group by displayname
112
-	 *
113
-	 * @param string $search
114
-	 * @param int $limit
115
-	 * @param int $offset
116
-	 * @return \OCP\IUser[]
117
-	 * @since 8.0.0
118
-	 */
119
-	public function searchDisplayName($search, $limit = null, $offset = null);
110
+    /**
111
+     * search for users in the group by displayname
112
+     *
113
+     * @param string $search
114
+     * @param int $limit
115
+     * @param int $offset
116
+     * @return \OCP\IUser[]
117
+     * @since 8.0.0
118
+     */
119
+    public function searchDisplayName($search, $limit = null, $offset = null);
120 120
 
121
-	/**
122
-	 * delete the group
123
-	 *
124
-	 * @return bool
125
-	 * @since 8.0.0
126
-	 */
127
-	public function delete();
121
+    /**
122
+     * delete the group
123
+     *
124
+     * @return bool
125
+     * @since 8.0.0
126
+     */
127
+    public function delete();
128 128
 }
Please login to merge, or discard this patch.