forked from moodlehq/matrixsynapse_mock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f81ccc
commit 4562795
Showing
11 changed files
with
399 additions
and
110 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace App\Controller; | ||
|
||
use App\Entity\Passwords; | ||
use App\Entity\Tokens; | ||
use App\Entity\Users; | ||
use App\Traits\GeneralTrait; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
|
||
/** | ||
* @Route("/{serverID}/backoffice") | ||
*/ | ||
class BackOfficeController extends AbstractController { | ||
|
||
use GeneralTrait; | ||
|
||
/** | ||
* Create admin user. | ||
* | ||
* @Route("/create-admin", name="createAdmin") | ||
* @param string $serverID | ||
* @param Request $request | ||
* @return JsonResponse | ||
*/ | ||
public function createAdmin(string $serverID, Request $request) { | ||
$method = $request->getMethod(); | ||
if ($method === 'POST') { | ||
$entityManager = $this->getDoctrine()->getManager(); | ||
|
||
$user = $entityManager->getRepository(Users::class)->findOneBy(['userid' => '@admin:synapse']); | ||
if (!$user) { | ||
$user = new Users(); | ||
$user->setServerid($serverID); | ||
$user->setUserid('@admin:synapse'); | ||
$user->setDisplayname('Admin User'); | ||
$user->setAdmin(true); | ||
} | ||
|
||
// Process tokens. | ||
$token = $entityManager->getRepository(Tokens::class) | ||
->findOneBy(['userid' => $user->getId()]); | ||
if (!$token) { | ||
// New user, or existing user without any associated Tokens. | ||
$token = new Tokens(); | ||
$token->setAccesstoken($this->generateToken('access-token')); | ||
$token->setExpiresinms(); | ||
$token->setServerid($serverID); | ||
|
||
$user->addtoken($token); | ||
$token->setUserid($user); | ||
$entityManager->persist($token); | ||
} | ||
|
||
// Process password. | ||
$passwords = $entityManager->getRepository(Passwords::class) | ||
->findOneBy(['userid' => $user->getId()]); | ||
if (!$passwords) { | ||
// 1. Generates and returns token as password. | ||
// 2. Generates and returns token pattern. | ||
$password = $this->hashPassword('password', null, true); | ||
|
||
// New user, or existing user without any associated Tokens. | ||
$passwords = new Passwords(); | ||
$passwords->setPassword($password['token']); | ||
|
||
$user->addPasswords($passwords); | ||
$user->setPasswordpattern($password['pattern']); | ||
$passwords->setUserid($user); | ||
$entityManager->persist($passwords); | ||
} | ||
$entityManager->persist($user); | ||
$entityManager->flush(); | ||
|
||
return new JsonResponse((object)[ | ||
'user_id' => $user->getUserid(), | ||
'password' => 'password' | ||
], 200); | ||
} else { | ||
return new JsonResponse( | ||
'Only POST method is allowed.', | ||
403 | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.