Blame view

sources/apps/user_ldap/lib/jobs.php 6.21 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  <?php
  
  /**
   * ownCloud – LDAP Background Jobs
   *
   * @author Arthur Schiwon
   * @copyright 2012 Arthur Schiwon blizzz@owncloud.com
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
   *
   * You should have received a copy of the GNU Affero General Public
   * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
   *
   */
  
  namespace OCA\user_ldap\lib;
31b7f2792   Kload   Upgrade to ownclo...
25
  class Jobs extends \OC\BackgroundJob\TimedJob {
03e52840d   Kload   Init
26
27
28
29
  	static private $groupsFromDB;
  
  	static private $groupBE;
  	static private $connector;
31b7f2792   Kload   Upgrade to ownclo...
30
31
32
  	public function __construct(){
  		$this->interval = self::getRefreshInterval();
  	}
6d9380f96   Cédric Dupont   Update sources OC...
33
34
35
  	/**
  	 * @param mixed $argument
  	 */
31b7f2792   Kload   Upgrade to ownclo...
36
37
38
  	public function run($argument){
  		Jobs::updateGroups();
  	}
03e52840d   Kload   Init
39
40
  	static public function updateGroups() {
  		\OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', \OCP\Util::DEBUG);
03e52840d   Kload   Init
41
42
43
44
45
46
47
48
  
  		$knownGroups = array_keys(self::getKnownGroups());
  		$actualGroups = self::getGroupBE()->getGroups();
  
  		if(empty($actualGroups) && empty($knownGroups)) {
  			\OCP\Util::writeLog('user_ldap',
  				'bgJ "updateGroups" – groups do not seem to be configured properly, aborting.',
  				\OCP\Util::INFO);
03e52840d   Kload   Init
49
50
51
52
53
54
  			return;
  		}
  
  		self::handleKnownGroups(array_intersect($actualGroups, $knownGroups));
  		self::handleCreatedGroups(array_diff($actualGroups, $knownGroups));
  		self::handleRemovedGroups(array_diff($knownGroups, $actualGroups));
03e52840d   Kload   Init
55
56
  		\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', \OCP\Util::DEBUG);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
57
58
59
  	/**
  	 * @return int
  	 */
03e52840d   Kload   Init
60
61
62
63
  	static private function getRefreshInterval() {
  		//defaults to every hour
  		return \OCP\Config::getAppValue('user_ldap', 'bgjRefreshInterval', 3600);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
64
65
66
  	/**
  	 * @param string[] $groups
  	 */
03e52840d   Kload   Init
67
68
69
70
71
72
73
74
75
  	static private function handleKnownGroups($groups) {
  		\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', \OCP\Util::DEBUG);
  		$query = \OCP\DB::prepare('
  			UPDATE `*PREFIX*ldap_group_members`
  			SET `owncloudusers` = ?
  			WHERE `owncloudname` = ?
  		');
  		foreach($groups as $group) {
  			//we assume, that self::$groupsFromDB has been retrieved already
6d9380f96   Cédric Dupont   Update sources OC...
76
77
78
79
80
81
  			$knownUsers = unserialize(self::$groupsFromDB[$group]['owncloudusers']);
  			$actualUsers = self::getGroupBE()->usersInGroup($group);
  			$hasChanged = false;
  			foreach(array_diff($knownUsers, $actualUsers) as $removedUser) {
  				\OCP\Util::emitHook('OC_User', 'post_removeFromGroup', array('uid' => $removedUser, 'gid' => $group));
  				\OCP\Util::writeLog('user_ldap',
03e52840d   Kload   Init
82
83
  				'bgJ "updateGroups" – "'.$removedUser.'" removed from "'.$group.'".',
  				\OCP\Util::INFO);
6d9380f96   Cédric Dupont   Update sources OC...
84
85
86
87
88
  				$hasChanged = true;
  			}
  			foreach(array_diff($actualUsers, $knownUsers) as $addedUser) {
  				\OCP\Util::emitHook('OC_User', 'post_addToGroup', array('uid' => $addedUser, 'gid' => $group));
  				\OCP\Util::writeLog('user_ldap',
03e52840d   Kload   Init
89
90
  				'bgJ "updateGroups" – "'.$addedUser.'" added to "'.$group.'".',
  				\OCP\Util::INFO);
6d9380f96   Cédric Dupont   Update sources OC...
91
92
93
  				$hasChanged = true;
  			}
  			if($hasChanged) {
03e52840d   Kload   Init
94
  				$query->execute(array(serialize($actualUsers), $group));
6d9380f96   Cédric Dupont   Update sources OC...
95
  			}
03e52840d   Kload   Init
96
97
98
99
100
  		}
  		\OCP\Util::writeLog('user_ldap',
  			'bgJ "updateGroups" – FINISHED dealing with known Groups.',
  			\OCP\Util::DEBUG);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
101
102
103
  	/**
  	 * @param string[] $createdGroups
  	 */
03e52840d   Kload   Init
104
105
106
107
108
109
110
111
112
113
114
115
  	static private function handleCreatedGroups($createdGroups) {
  		\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', \OCP\Util::DEBUG);
  		$query = \OCP\DB::prepare('
  			INSERT
  			INTO `*PREFIX*ldap_group_members` (`owncloudname`, `owncloudusers`)
  			VALUES (?, ?)
  		');
  		foreach($createdGroups as $createdGroup) {
  			\OCP\Util::writeLog('user_ldap',
  				'bgJ "updateGroups" – new group "'.$createdGroup.'" found.',
  				\OCP\Util::INFO);
  			$users = serialize(self::getGroupBE()->usersInGroup($createdGroup));
6d9380f96   Cédric Dupont   Update sources OC...
116
  			$query->execute(array($createdGroup, $users));
03e52840d   Kload   Init
117
118
119
120
121
  		}
  		\OCP\Util::writeLog('user_ldap',
  			'bgJ "updateGroups" – FINISHED dealing with created Groups.',
  			\OCP\Util::DEBUG);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
122
123
124
  	/**
  	 * @param string[] $removedGroups
  	 */
03e52840d   Kload   Init
125
126
127
128
129
130
131
132
133
134
135
  	static private function handleRemovedGroups($removedGroups) {
  		\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', \OCP\Util::DEBUG);
  		$query = \OCP\DB::prepare('
  			DELETE
  			FROM `*PREFIX*ldap_group_members`
  			WHERE `owncloudname` = ?
  		');
  		foreach($removedGroups as $removedGroup) {
  			\OCP\Util::writeLog('user_ldap',
  				'bgJ "updateGroups" – group "'.$removedGroup.'" was removed.',
  				\OCP\Util::INFO);
6d9380f96   Cédric Dupont   Update sources OC...
136
  			$query->execute(array($removedGroup));
03e52840d   Kload   Init
137
138
139
140
141
  		}
  		\OCP\Util::writeLog('user_ldap',
  			'bgJ "updateGroups" – FINISHED dealing with removed groups.',
  			\OCP\Util::DEBUG);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
142
143
144
  	/**
  	 * @return \OCA\user_ldap\GROUP_LDAP|\OCA\user_ldap\Group_Proxy
  	 */
03e52840d   Kload   Init
145
146
147
148
149
  	static private function getGroupBE() {
  		if(!is_null(self::$groupBE)) {
  			return self::$groupBE;
  		}
  		$configPrefixes = Helper::getServerConfigurationPrefixes(true);
31b7f2792   Kload   Upgrade to ownclo...
150
151
  		$ldapWrapper = new LDAP();
  		if(count($configPrefixes) === 1) {
03e52840d   Kload   Init
152
  			//avoid the proxy when there is only one LDAP server configured
6d9380f96   Cédric Dupont   Update sources OC...
153
154
155
156
157
158
  			$userManager = new user\Manager(
  				\OC::$server->getConfig(),
  				new FilesystemHelper(),
  				new LogWrapper(),
  				\OC::$server->getAvatarManager(),
  				new \OCP\Image());
31b7f2792   Kload   Upgrade to ownclo...
159
  			$connector = new Connection($ldapWrapper, $configPrefixes[0]);
6d9380f96   Cédric Dupont   Update sources OC...
160
  			$ldapAccess = new Access($connector, $ldapWrapper, $userManager);
31b7f2792   Kload   Upgrade to ownclo...
161
  			self::$groupBE = new \OCA\user_ldap\GROUP_LDAP($ldapAccess);
03e52840d   Kload   Init
162
  		} else {
31b7f2792   Kload   Upgrade to ownclo...
163
  			self::$groupBE = new \OCA\user_ldap\Group_Proxy($configPrefixes, $ldapWrapper);
03e52840d   Kload   Init
164
165
166
167
  		}
  
  		return self::$groupBE;
  	}
6d9380f96   Cédric Dupont   Update sources OC...
168
169
170
  	/**
  	 * @return array
  	 */
03e52840d   Kload   Init
171
172
173
174
175
176
177
178
179
180
181
  	static private function getKnownGroups() {
  		if(is_array(self::$groupsFromDB)) {
  			return self::$groupsFromDB;
  		}
  		$query = \OCP\DB::prepare('
  			SELECT `owncloudname`, `owncloudusers`
  			FROM `*PREFIX*ldap_group_members`
  		');
  		$result = $query->execute()->fetchAll();
  		self::$groupsFromDB = array();
  		foreach($result as $dataset) {
6d9380f96   Cédric Dupont   Update sources OC...
182
  			self::$groupsFromDB[$dataset['owncloudname']] = $dataset;
03e52840d   Kload   Init
183
184
185
186
187
  		}
  
  		return self::$groupsFromDB;
  	}
  }