public function setUser(User $user) { if (!$user->getId()) { throw new UserRequiredException(); } $this->user = $user; }
/** * Set a particular atom on a user * * @param User $user * @param $key * @param $value */ public function setAtom(User $user, $key, $value) { $conn = $this->getEntityManager()->getConnection(); $stmt = $conn->prepare('INSERT INTO users_atoms ( user_id, `key`, `value`) VALUES( ?, ?, ? ) ON DUPLICATE KEY UPDATE `value`=VALUES(`value`)'); $user_id = $user->getId(); $stmt->bindParam(1, $user_id); $stmt->bindParam(2, $key); $stmt->bindParam(3, $value); $stmt->execute(); }
function it_should_not_panic_when_guards_are_not_defined(ServiceManager $serviceManager, RoleMapper $roleMapper, GroupPermissionProviderInterface $ruleMapper, UserPermissionProviderInterface $userActionRuleMapper, AuthenticationService $authenticationService, User $user, UserMapper $userMapper) { $config = ['circlical' => ['user' => ['providers' => ['role' => RoleMapper::class, 'rule' => ['group' => GroupPermissionProviderInterface::class, 'user' => UserPermissionProviderInterface::class]]]]]; $user->getId()->willReturn(1); $authenticationService->getIdentity()->willReturn($user); $serviceManager->get('config')->willReturn($config); $serviceManager->get(RoleMapper::class)->willReturn($roleMapper); $serviceManager->get(GroupPermissionProviderInterface::class)->willReturn($ruleMapper); $serviceManager->get(UserPermissionProviderInterface::class)->willReturn($userActionRuleMapper); $serviceManager->get(AuthenticationService::class)->willReturn($authenticationService); $serviceManager->get(UserMapper::class)->willReturn($userMapper); $this->shouldThrow(\Exception::class)->during('__invoke', [$serviceManager, AccessService::class]); }
public function it_requires_that_users_have_id_during_creation(User $otherUser) { $otherUser->getId()->willReturn(null); $this->shouldThrow(PersistedUserRequiredException::class)->during('create', [$otherUser, 'whoami', 'nobody']); }
/** * Very similar to create, except that it won't log the user in. This was created to satisfy circumstances where * you are creating users from an admin panel for example. This function is also used by create. * * @param User $user * @param string $username * @param string $password * * @return AuthenticationRecordInterface * @throws EmailUsernameTakenException * @throws MismatchedEmailsException * @throws PersistedUserRequiredException * @throws UsernameTakenException */ public function registerAuthenticationRecord(User $user, string $username, string $password) : AuthenticationRecordInterface { if (!$user->getId()) { throw new PersistedUserRequiredException("Your user must have an ID before you can create auth records with it"); } if ($this->authenticationProvider->findByUsername($username)) { throw new UsernameTakenException(); } if (filter_var($username, FILTER_VALIDATE_EMAIL)) { if ($user->getEmail() != $username) { throw new MismatchedEmailsException(); } if ($emailUser = $this->userProvider->findByEmail($username)) { if ($emailUser != $user) { throw new EmailUsernameTakenException(); } } } $hash = password_hash($password, PASSWORD_DEFAULT); $auth = $this->authenticationProvider->create($user->getId(), $username, $hash, KeyFactory::generateEncryptionKey()->getRawKeyMaterial()); $this->authenticationProvider->save($auth); return $auth; }
function it_rejects_users_with_no_id(User $noUser) { $noUser->getId()->willReturn(null); $this->shouldThrow(UserRequiredException::class)->during('setUser', [$noUser]); }