Skip to content

Commit b13628a

Browse files
committed
Fix issuing temporary access tokens
Previously it wasn’t checked if a user existed and was always created. This caused an error on creating a temporary access token for the second time for a user. This commit fixes the problem by checking if a user exists before attempting to create it.
1 parent 080f55f commit b13628a

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

contributors.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
* Jakob Murko - @sraka1
22
* Joerg Boeselt - @RoboSparrow
3+
* Maarten de Boer - @mdeboer

src/xAPI/Service/Auth/Basic.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,14 @@ public function accessTokenPost()
173173

174174
// TODO 0.11.x: This functionality (user creation + token creation should be in two separate API calls)
175175
$userService = new UserService($this->getContainer());
176-
$user = $userService->addUser($parsedParams->user->name, $parsedParams->user->description, $parsedParams->user->email, $parsedParams->user->password, $permissionDocuments)->toArray();
176+
177+
// Fetch user or create it if it doesn't exist
178+
$user = $userService->getStorage()->getUserStorage()->findByEmail($parsedParams->user->email);
179+
180+
if ($user === null) {
181+
$user = $userService->addUser($parsedParams->user->name, $parsedParams->user->description, $parsedParams->user->email, $parsedParams->user->password, $permissionDocuments)->toArray();
182+
}
183+
177184
$accessTokenDocument = $this->addToken($parsedParams->name, $parsedParams->description, $expiresAt, $user, $scopeDocuments);
178185

179186
return $accessTokenDocument;

src/xAPI/Storage/Adapter/Mongo/User.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,19 @@ public function findByEmailAndPassword($username, $password)
175175

176176
return $document;
177177
}
178+
179+
/**
180+
* {@inheritDoc}
181+
*/
182+
public function findByEmail($username)
183+
{
184+
$storage = $this->getContainer()->get('storage');
185+
$expression = $storage->createExpression();
186+
187+
$expression->where('email', $username);
188+
189+
$document = $storage->findOne(self::COLLECTION_NAME, $expression);
190+
191+
return $document;
192+
}
178193
}

0 commit comments

Comments
 (0)