Skip to content

Instantly share code, notes, and snippets.

@hightemp
Last active May 17, 2023 12:18
Show Gist options
  • Save hightemp/e22e516f4cffdbecf595c5cecf1e0d3c to your computer and use it in GitHub Desktop.
Save hightemp/e22e516f4cffdbecf595c5cecf1e0d3c to your computer and use it in GitHub Desktop.
laminas create auth2 with user
<?php
namespace ReferenceBooks\Controller;
use ApiSkeletons\OAuth2\Doctrine\Entity\AccessToken;
use ApiSkeletons\OAuth2\Doctrine\Entity\Client;
use Doctrine\ORM\EntityManager;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\JsonModel;
use Users\Entity\Acl\Role;
use Users\Entity\User;
class IndexController extends AbstractActionController
{
/**
* @var EntityManager
*/
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function indexAction()
{
return new JsonModel(['success' => true]);
}
public function createUserAction()
{
$clientId = "test";
$adminLevel = USER::ADMIN_LEVEL_SUPERADMIN;
$aclRole = Role::USER;
// $role = (new Role())->setRoleId($aclRole);
$role = $this->em->getRepository(Role::class)->findOneBy(['roleId' => $aclRole]);
// $role = (Role) $role;
$user = (new User())
->setPhone('+9999999999')
->setStatus(true)
->setFirstName('Alexandr')
->setLastName('Pushkin')
->setEmail('[email protected]')
->setUsername("un123" . rand())
->setAdminLevelRpc($adminLevel)
->addRole($role);
$this->em->persist($user);
$role->addUser($user);
$this->em->persist($role);
$client = (new Client())
->setClientId($clientId)
->setGrantType(['password']);
$this->em->persist($client);
$accessToken = (new AccessToken())
->exchangeArray([
'user' => $user,
'accessToken' => "test",
'expires' => new \DateTime('today +1 year'),
'client' => $client
]);
$this->em->persist($accessToken);
$this->em->flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment