@@ -38,170 +38,170 @@ |
||
| 38 | 38 | */ |
| 39 | 39 | class AccountManager { |
| 40 | 40 | |
| 41 | - /** nobody can see my account details */ |
|
| 42 | - const VISIBILITY_PRIVATE = 'private'; |
|
| 43 | - /** only contacts, especially trusted servers can see my contact details */ |
|
| 44 | - const VISIBILITY_CONTACTS_ONLY = 'contacts'; |
|
| 45 | - /** every body ca see my contact detail, will be published to the lookup server */ |
|
| 46 | - const VISIBILITY_PUBLIC = 'public'; |
|
| 47 | - |
|
| 48 | - const PROPERTY_AVATAR = 'avatar'; |
|
| 49 | - const PROPERTY_DISPLAYNAME = 'displayname'; |
|
| 50 | - const PROPERTY_PHONE = 'phone'; |
|
| 51 | - const PROPERTY_EMAIL = 'email'; |
|
| 52 | - const PROPERTY_WEBSITE = 'website'; |
|
| 53 | - const PROPERTY_ADDRESS = 'address'; |
|
| 54 | - const PROPERTY_TWITTER = 'twitter'; |
|
| 55 | - |
|
| 56 | - /** @var IDBConnection database connection */ |
|
| 57 | - private $connection; |
|
| 58 | - |
|
| 59 | - /** @var string table name */ |
|
| 60 | - private $table = 'accounts'; |
|
| 61 | - |
|
| 62 | - /** @var EventDispatcherInterface */ |
|
| 63 | - private $eventDispatcher; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * AccountManager constructor. |
|
| 67 | - * |
|
| 68 | - * @param IDBConnection $connection |
|
| 69 | - * @param EventDispatcherInterface $eventDispatcher |
|
| 70 | - */ |
|
| 71 | - public function __construct(IDBConnection $connection, EventDispatcherInterface $eventDispatcher) { |
|
| 72 | - $this->connection = $connection; |
|
| 73 | - $this->eventDispatcher = $eventDispatcher; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * update user record |
|
| 78 | - * |
|
| 79 | - * @param IUser $user |
|
| 80 | - * @param $data |
|
| 81 | - */ |
|
| 82 | - public function updateUser(IUser $user, $data) { |
|
| 83 | - $userData = $this->getUser($user); |
|
| 84 | - $updated = true; |
|
| 85 | - if (empty($userData)) { |
|
| 86 | - $this->insertNewUser($user, $data); |
|
| 87 | - } elseif ($userData !== $data) { |
|
| 88 | - $this->updateExistingUser($user, $data); |
|
| 89 | - } else { |
|
| 90 | - // nothing needs to be done if new and old data set are the same |
|
| 91 | - $updated = false; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - if ($updated) { |
|
| 95 | - $this->eventDispatcher->dispatch( |
|
| 96 | - 'OC\AccountManager::userUpdated', |
|
| 97 | - new GenericEvent($user, $data) |
|
| 98 | - ); |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * get stored data from a given user |
|
| 104 | - * |
|
| 105 | - * @param IUser $user |
|
| 106 | - * @return array |
|
| 107 | - */ |
|
| 108 | - public function getUser(IUser $user) { |
|
| 109 | - $uid = $user->getUID(); |
|
| 110 | - $query = $this->connection->getQueryBuilder(); |
|
| 111 | - $query->select('data')->from($this->table) |
|
| 112 | - ->where($query->expr()->eq('uid', $query->createParameter('uid'))) |
|
| 113 | - ->setParameter('uid', $uid); |
|
| 114 | - $query->execute(); |
|
| 115 | - $result = $query->execute()->fetchAll(); |
|
| 116 | - |
|
| 117 | - if (empty($result)) { |
|
| 118 | - $userData = $this->buildDefaultUserRecord($user); |
|
| 119 | - $this->insertNewUser($user, $userData); |
|
| 120 | - return $userData; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - return json_decode($result[0]['data'], true); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * add new user to accounts table |
|
| 128 | - * |
|
| 129 | - * @param IUser $user |
|
| 130 | - * @param array $data |
|
| 131 | - */ |
|
| 132 | - protected function insertNewUser(IUser $user, $data) { |
|
| 133 | - $uid = $user->getUID(); |
|
| 134 | - $jsonEncodedData = json_encode($data); |
|
| 135 | - $query = $this->connection->getQueryBuilder(); |
|
| 136 | - $query->insert($this->table) |
|
| 137 | - ->values( |
|
| 138 | - [ |
|
| 139 | - 'uid' => $query->createNamedParameter($uid), |
|
| 140 | - 'data' => $query->createNamedParameter($jsonEncodedData), |
|
| 141 | - ] |
|
| 142 | - ) |
|
| 143 | - ->execute(); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * update existing user in accounts table |
|
| 148 | - * |
|
| 149 | - * @param IUser $user |
|
| 150 | - * @param array $data |
|
| 151 | - */ |
|
| 152 | - protected function updateExistingUser(IUser $user, $data) { |
|
| 153 | - $uid = $user->getUID(); |
|
| 154 | - $jsonEncodedData = json_encode($data); |
|
| 155 | - $query = $this->connection->getQueryBuilder(); |
|
| 156 | - $query->update($this->table) |
|
| 157 | - ->set('data', $query->createNamedParameter($jsonEncodedData)) |
|
| 158 | - ->where($query->expr()->eq('uid', $query->createNamedParameter($uid))) |
|
| 159 | - ->execute(); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * build default user record in case not data set exists yet |
|
| 164 | - * |
|
| 165 | - * @param IUser $user |
|
| 166 | - * @return array |
|
| 167 | - */ |
|
| 168 | - protected function buildDefaultUserRecord(IUser $user) { |
|
| 169 | - return [ |
|
| 170 | - self::PROPERTY_DISPLAYNAME => |
|
| 171 | - [ |
|
| 172 | - 'value' => $user->getDisplayName(), |
|
| 173 | - 'scope' => self::VISIBILITY_CONTACTS_ONLY, |
|
| 174 | - ], |
|
| 175 | - self::PROPERTY_ADDRESS => |
|
| 176 | - [ |
|
| 177 | - 'value' => '', |
|
| 178 | - 'scope' => self::VISIBILITY_PRIVATE, |
|
| 179 | - ], |
|
| 180 | - self::PROPERTY_WEBSITE => |
|
| 181 | - [ |
|
| 182 | - 'value' => '', |
|
| 183 | - 'scope' => self::VISIBILITY_PRIVATE, |
|
| 184 | - ], |
|
| 185 | - self::PROPERTY_EMAIL => |
|
| 186 | - [ |
|
| 187 | - 'value' => $user->getEMailAddress(), |
|
| 188 | - 'scope' => self::VISIBILITY_CONTACTS_ONLY, |
|
| 189 | - ], |
|
| 190 | - self::PROPERTY_AVATAR => |
|
| 191 | - [ |
|
| 192 | - 'scope' => self::VISIBILITY_CONTACTS_ONLY |
|
| 193 | - ], |
|
| 194 | - self::PROPERTY_PHONE => |
|
| 195 | - [ |
|
| 196 | - 'value' => '', |
|
| 197 | - 'scope' => self::VISIBILITY_PRIVATE, |
|
| 198 | - ], |
|
| 199 | - self::PROPERTY_TWITTER => |
|
| 200 | - [ |
|
| 201 | - 'value' => '', |
|
| 202 | - 'scope' => self::VISIBILITY_PRIVATE, |
|
| 203 | - ], |
|
| 204 | - ]; |
|
| 205 | - } |
|
| 41 | + /** nobody can see my account details */ |
|
| 42 | + const VISIBILITY_PRIVATE = 'private'; |
|
| 43 | + /** only contacts, especially trusted servers can see my contact details */ |
|
| 44 | + const VISIBILITY_CONTACTS_ONLY = 'contacts'; |
|
| 45 | + /** every body ca see my contact detail, will be published to the lookup server */ |
|
| 46 | + const VISIBILITY_PUBLIC = 'public'; |
|
| 47 | + |
|
| 48 | + const PROPERTY_AVATAR = 'avatar'; |
|
| 49 | + const PROPERTY_DISPLAYNAME = 'displayname'; |
|
| 50 | + const PROPERTY_PHONE = 'phone'; |
|
| 51 | + const PROPERTY_EMAIL = 'email'; |
|
| 52 | + const PROPERTY_WEBSITE = 'website'; |
|
| 53 | + const PROPERTY_ADDRESS = 'address'; |
|
| 54 | + const PROPERTY_TWITTER = 'twitter'; |
|
| 55 | + |
|
| 56 | + /** @var IDBConnection database connection */ |
|
| 57 | + private $connection; |
|
| 58 | + |
|
| 59 | + /** @var string table name */ |
|
| 60 | + private $table = 'accounts'; |
|
| 61 | + |
|
| 62 | + /** @var EventDispatcherInterface */ |
|
| 63 | + private $eventDispatcher; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * AccountManager constructor. |
|
| 67 | + * |
|
| 68 | + * @param IDBConnection $connection |
|
| 69 | + * @param EventDispatcherInterface $eventDispatcher |
|
| 70 | + */ |
|
| 71 | + public function __construct(IDBConnection $connection, EventDispatcherInterface $eventDispatcher) { |
|
| 72 | + $this->connection = $connection; |
|
| 73 | + $this->eventDispatcher = $eventDispatcher; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * update user record |
|
| 78 | + * |
|
| 79 | + * @param IUser $user |
|
| 80 | + * @param $data |
|
| 81 | + */ |
|
| 82 | + public function updateUser(IUser $user, $data) { |
|
| 83 | + $userData = $this->getUser($user); |
|
| 84 | + $updated = true; |
|
| 85 | + if (empty($userData)) { |
|
| 86 | + $this->insertNewUser($user, $data); |
|
| 87 | + } elseif ($userData !== $data) { |
|
| 88 | + $this->updateExistingUser($user, $data); |
|
| 89 | + } else { |
|
| 90 | + // nothing needs to be done if new and old data set are the same |
|
| 91 | + $updated = false; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + if ($updated) { |
|
| 95 | + $this->eventDispatcher->dispatch( |
|
| 96 | + 'OC\AccountManager::userUpdated', |
|
| 97 | + new GenericEvent($user, $data) |
|
| 98 | + ); |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * get stored data from a given user |
|
| 104 | + * |
|
| 105 | + * @param IUser $user |
|
| 106 | + * @return array |
|
| 107 | + */ |
|
| 108 | + public function getUser(IUser $user) { |
|
| 109 | + $uid = $user->getUID(); |
|
| 110 | + $query = $this->connection->getQueryBuilder(); |
|
| 111 | + $query->select('data')->from($this->table) |
|
| 112 | + ->where($query->expr()->eq('uid', $query->createParameter('uid'))) |
|
| 113 | + ->setParameter('uid', $uid); |
|
| 114 | + $query->execute(); |
|
| 115 | + $result = $query->execute()->fetchAll(); |
|
| 116 | + |
|
| 117 | + if (empty($result)) { |
|
| 118 | + $userData = $this->buildDefaultUserRecord($user); |
|
| 119 | + $this->insertNewUser($user, $userData); |
|
| 120 | + return $userData; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + return json_decode($result[0]['data'], true); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * add new user to accounts table |
|
| 128 | + * |
|
| 129 | + * @param IUser $user |
|
| 130 | + * @param array $data |
|
| 131 | + */ |
|
| 132 | + protected function insertNewUser(IUser $user, $data) { |
|
| 133 | + $uid = $user->getUID(); |
|
| 134 | + $jsonEncodedData = json_encode($data); |
|
| 135 | + $query = $this->connection->getQueryBuilder(); |
|
| 136 | + $query->insert($this->table) |
|
| 137 | + ->values( |
|
| 138 | + [ |
|
| 139 | + 'uid' => $query->createNamedParameter($uid), |
|
| 140 | + 'data' => $query->createNamedParameter($jsonEncodedData), |
|
| 141 | + ] |
|
| 142 | + ) |
|
| 143 | + ->execute(); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * update existing user in accounts table |
|
| 148 | + * |
|
| 149 | + * @param IUser $user |
|
| 150 | + * @param array $data |
|
| 151 | + */ |
|
| 152 | + protected function updateExistingUser(IUser $user, $data) { |
|
| 153 | + $uid = $user->getUID(); |
|
| 154 | + $jsonEncodedData = json_encode($data); |
|
| 155 | + $query = $this->connection->getQueryBuilder(); |
|
| 156 | + $query->update($this->table) |
|
| 157 | + ->set('data', $query->createNamedParameter($jsonEncodedData)) |
|
| 158 | + ->where($query->expr()->eq('uid', $query->createNamedParameter($uid))) |
|
| 159 | + ->execute(); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * build default user record in case not data set exists yet |
|
| 164 | + * |
|
| 165 | + * @param IUser $user |
|
| 166 | + * @return array |
|
| 167 | + */ |
|
| 168 | + protected function buildDefaultUserRecord(IUser $user) { |
|
| 169 | + return [ |
|
| 170 | + self::PROPERTY_DISPLAYNAME => |
|
| 171 | + [ |
|
| 172 | + 'value' => $user->getDisplayName(), |
|
| 173 | + 'scope' => self::VISIBILITY_CONTACTS_ONLY, |
|
| 174 | + ], |
|
| 175 | + self::PROPERTY_ADDRESS => |
|
| 176 | + [ |
|
| 177 | + 'value' => '', |
|
| 178 | + 'scope' => self::VISIBILITY_PRIVATE, |
|
| 179 | + ], |
|
| 180 | + self::PROPERTY_WEBSITE => |
|
| 181 | + [ |
|
| 182 | + 'value' => '', |
|
| 183 | + 'scope' => self::VISIBILITY_PRIVATE, |
|
| 184 | + ], |
|
| 185 | + self::PROPERTY_EMAIL => |
|
| 186 | + [ |
|
| 187 | + 'value' => $user->getEMailAddress(), |
|
| 188 | + 'scope' => self::VISIBILITY_CONTACTS_ONLY, |
|
| 189 | + ], |
|
| 190 | + self::PROPERTY_AVATAR => |
|
| 191 | + [ |
|
| 192 | + 'scope' => self::VISIBILITY_CONTACTS_ONLY |
|
| 193 | + ], |
|
| 194 | + self::PROPERTY_PHONE => |
|
| 195 | + [ |
|
| 196 | + 'value' => '', |
|
| 197 | + 'scope' => self::VISIBILITY_PRIVATE, |
|
| 198 | + ], |
|
| 199 | + self::PROPERTY_TWITTER => |
|
| 200 | + [ |
|
| 201 | + 'value' => '', |
|
| 202 | + 'scope' => self::VISIBILITY_PRIVATE, |
|
| 203 | + ], |
|
| 204 | + ]; |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | 207 | } |