Passed
Push — master ( f552f2...38e534 )
by Morris
70:25 queued 54:09
created
apps/files_external/lib/Lib/Auth/Password/LoginCredentials.php 2 patches
Indentation   +93 added lines, -93 removed lines patch added patch discarded remove patch
@@ -45,97 +45,97 @@
 block discarded – undo
45 45
  * Username and password from login credentials, saved in DB
46 46
  */
47 47
 class LoginCredentials extends AuthMechanism {
48
-	public const CREDENTIALS_IDENTIFIER = 'password::logincredentials/credentials';
49
-
50
-	/** @var ISession */
51
-	protected $session;
52
-
53
-	/** @var ICredentialsManager */
54
-	protected $credentialsManager;
55
-
56
-	/** @var CredentialsStore */
57
-	private $credentialsStore;
58
-
59
-	public function __construct(IL10N $l, ISession $session, ICredentialsManager $credentialsManager, CredentialsStore $credentialsStore, IEventDispatcher $eventDispatcher) {
60
-		$this->session = $session;
61
-		$this->credentialsManager = $credentialsManager;
62
-		$this->credentialsStore = $credentialsStore;
63
-
64
-		$this
65
-			->setIdentifier('password::logincredentials')
66
-			->setScheme(self::SCHEME_PASSWORD)
67
-			->setText($l->t('Log-in credentials, save in database'))
68
-			->addParameters([
69
-			]);
70
-
71
-		$eventDispatcher->addServiceListener(UserLoggedInEvent::class, StorePasswordListener::class);
72
-		$eventDispatcher->addServiceListener(PasswordUpdatedEvent::class, StorePasswordListener::class);
73
-	}
74
-
75
-	private function getCredentials(IUser $user): array {
76
-		$credentials = $this->credentialsManager->retrieve($user->getUID(), self::CREDENTIALS_IDENTIFIER);
77
-
78
-		if (is_null($credentials)) {
79
-			// nothing saved in db, try to get it from the session and save it
80
-			try {
81
-				$sessionCredentials = $this->credentialsStore->getLoginCredentials();
82
-
83
-				$credentials = [
84
-					'user' => $sessionCredentials->getLoginName(),
85
-					'password' => $sessionCredentials->getPassword(),
86
-				];
87
-
88
-				$this->credentialsManager->store($user->getUID(), self::CREDENTIALS_IDENTIFIER, $credentials);
89
-			} catch (CredentialsUnavailableException $e) {
90
-				throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved');
91
-			}
92
-		}
93
-
94
-		return $credentials;
95
-	}
96
-
97
-	public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) {
98
-		if (!isset($user)) {
99
-			throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved');
100
-		}
101
-		$credentials = $this->getCredentials($user);
102
-
103
-		$loginKey = $storage->getBackendOption("login_ldap_attr");
104
-		if ($loginKey) {
105
-			$backend = $user->getBackend();
106
-			if ($backend instanceof IUserLDAP) {
107
-				$value = $this->getLdapPropertyForUser($backend, $user, $loginKey);
108
-				if ($value === null) {
109
-					throw new InsufficientDataForMeaningfulAnswerException('Custom ldap attribute not set for user ' . $user->getUID());
110
-				}
111
-				$storage->setBackendOption('user', $value);
112
-			} else {
113
-				throw new InsufficientDataForMeaningfulAnswerException('Custom ldap attribute configured but user ' . $user->getUID() . ' is not an ldap user');
114
-			}
115
-		} else {
116
-			$storage->setBackendOption('user', $credentials['user']);
117
-		}
118
-		$storage->setBackendOption('password', $credentials['password']);
119
-	}
120
-
121
-	private function getLdapPropertyForUser(IUserLDAP $ldap, IUser $user, string $property): ?string {
122
-		$access = $ldap->getLDAPAccess($user->getUID());
123
-		$connection = $access->getConnection();
124
-		$key = "external_login::" . $user->getUID() . "::" . $property;
125
-		$cached = $connection->getFromCache($key);
126
-
127
-		if ($cached !== null) {
128
-			return $cached;
129
-		}
130
-
131
-		$value = $access->readAttribute($access->username2dn($user->getUID()), $property);
132
-		if (count($value) > 0) {
133
-			$value = current($value);
134
-		} else {
135
-			return null;
136
-		}
137
-		$connection->writeToCache($key, $value);
138
-
139
-		return $value;
140
-	}
48
+    public const CREDENTIALS_IDENTIFIER = 'password::logincredentials/credentials';
49
+
50
+    /** @var ISession */
51
+    protected $session;
52
+
53
+    /** @var ICredentialsManager */
54
+    protected $credentialsManager;
55
+
56
+    /** @var CredentialsStore */
57
+    private $credentialsStore;
58
+
59
+    public function __construct(IL10N $l, ISession $session, ICredentialsManager $credentialsManager, CredentialsStore $credentialsStore, IEventDispatcher $eventDispatcher) {
60
+        $this->session = $session;
61
+        $this->credentialsManager = $credentialsManager;
62
+        $this->credentialsStore = $credentialsStore;
63
+
64
+        $this
65
+            ->setIdentifier('password::logincredentials')
66
+            ->setScheme(self::SCHEME_PASSWORD)
67
+            ->setText($l->t('Log-in credentials, save in database'))
68
+            ->addParameters([
69
+            ]);
70
+
71
+        $eventDispatcher->addServiceListener(UserLoggedInEvent::class, StorePasswordListener::class);
72
+        $eventDispatcher->addServiceListener(PasswordUpdatedEvent::class, StorePasswordListener::class);
73
+    }
74
+
75
+    private function getCredentials(IUser $user): array {
76
+        $credentials = $this->credentialsManager->retrieve($user->getUID(), self::CREDENTIALS_IDENTIFIER);
77
+
78
+        if (is_null($credentials)) {
79
+            // nothing saved in db, try to get it from the session and save it
80
+            try {
81
+                $sessionCredentials = $this->credentialsStore->getLoginCredentials();
82
+
83
+                $credentials = [
84
+                    'user' => $sessionCredentials->getLoginName(),
85
+                    'password' => $sessionCredentials->getPassword(),
86
+                ];
87
+
88
+                $this->credentialsManager->store($user->getUID(), self::CREDENTIALS_IDENTIFIER, $credentials);
89
+            } catch (CredentialsUnavailableException $e) {
90
+                throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved');
91
+            }
92
+        }
93
+
94
+        return $credentials;
95
+    }
96
+
97
+    public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) {
98
+        if (!isset($user)) {
99
+            throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved');
100
+        }
101
+        $credentials = $this->getCredentials($user);
102
+
103
+        $loginKey = $storage->getBackendOption("login_ldap_attr");
104
+        if ($loginKey) {
105
+            $backend = $user->getBackend();
106
+            if ($backend instanceof IUserLDAP) {
107
+                $value = $this->getLdapPropertyForUser($backend, $user, $loginKey);
108
+                if ($value === null) {
109
+                    throw new InsufficientDataForMeaningfulAnswerException('Custom ldap attribute not set for user ' . $user->getUID());
110
+                }
111
+                $storage->setBackendOption('user', $value);
112
+            } else {
113
+                throw new InsufficientDataForMeaningfulAnswerException('Custom ldap attribute configured but user ' . $user->getUID() . ' is not an ldap user');
114
+            }
115
+        } else {
116
+            $storage->setBackendOption('user', $credentials['user']);
117
+        }
118
+        $storage->setBackendOption('password', $credentials['password']);
119
+    }
120
+
121
+    private function getLdapPropertyForUser(IUserLDAP $ldap, IUser $user, string $property): ?string {
122
+        $access = $ldap->getLDAPAccess($user->getUID());
123
+        $connection = $access->getConnection();
124
+        $key = "external_login::" . $user->getUID() . "::" . $property;
125
+        $cached = $connection->getFromCache($key);
126
+
127
+        if ($cached !== null) {
128
+            return $cached;
129
+        }
130
+
131
+        $value = $access->readAttribute($access->username2dn($user->getUID()), $property);
132
+        if (count($value) > 0) {
133
+            $value = current($value);
134
+        } else {
135
+            return null;
136
+        }
137
+        $connection->writeToCache($key, $value);
138
+
139
+        return $value;
140
+    }
141 141
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -106,11 +106,11 @@  discard block
 block discarded – undo
106 106
 			if ($backend instanceof IUserLDAP) {
107 107
 				$value = $this->getLdapPropertyForUser($backend, $user, $loginKey);
108 108
 				if ($value === null) {
109
-					throw new InsufficientDataForMeaningfulAnswerException('Custom ldap attribute not set for user ' . $user->getUID());
109
+					throw new InsufficientDataForMeaningfulAnswerException('Custom ldap attribute not set for user '.$user->getUID());
110 110
 				}
111 111
 				$storage->setBackendOption('user', $value);
112 112
 			} else {
113
-				throw new InsufficientDataForMeaningfulAnswerException('Custom ldap attribute configured but user ' . $user->getUID() . ' is not an ldap user');
113
+				throw new InsufficientDataForMeaningfulAnswerException('Custom ldap attribute configured but user '.$user->getUID().' is not an ldap user');
114 114
 			}
115 115
 		} else {
116 116
 			$storage->setBackendOption('user', $credentials['user']);
@@ -121,7 +121,7 @@  discard block
 block discarded – undo
121 121
 	private function getLdapPropertyForUser(IUserLDAP $ldap, IUser $user, string $property): ?string {
122 122
 		$access = $ldap->getLDAPAccess($user->getUID());
123 123
 		$connection = $access->getConnection();
124
-		$key = "external_login::" . $user->getUID() . "::" . $property;
124
+		$key = "external_login::".$user->getUID()."::".$property;
125 125
 		$cached = $connection->getFromCache($key);
126 126
 
127 127
 		if ($cached !== null) {
Please login to merge, or discard this patch.