本文整理汇总了PHP中AppBundle\Entity\User::addRole方法的典型用法代码示例。如果您正苦于以下问题:PHP User::addRole方法的具体用法?PHP User::addRole怎么用?PHP User::addRole使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AppBundle\Entity\User
的用法示例。
在下文中一共展示了User::addRole方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: authenticateUser
/**
* @param User $user
* @param $role
*/
protected function authenticateUser(User $user, $role)
{
if ($role) {
if (is_array($role)) {
foreach ($role as $loopedRole) {
$user->addRole($loopedRole);
}
} else {
$user->addRole($role);
}
}
$token = new UsernamePasswordToken($user, 'dummy', $providerKey = 'main');
$this->authenticateToken($token);
}
示例2: injectUser
protected function injectUser()
{
$this->user = new User();
//$admin = 'tester'; // MWAHAHAHA
//$departmentId = 5; // UiB
$role = $this->em->getRepository('AppBundle:Role')->findOneByRole('ROLE_USER');
$this->user->addRole($role);
$this->user->setGender(1);
$this->user->setFirstName("Shi");
$this->user->setLastName("LaLong");
$emd = $this->em->getRepository('AppBundle:FieldOfStudy')->find(40);
// Tuple "Andre" from UiB, id 40 for fos
$this->user->setFieldOfStudy($emd);
$this->user->setUserName("shiluib");
// Weird typo?
$this->user->setEmail("shiluib@student.uib.no");
$this->user->setPhone("47661674");
$this->user->setPicturePath("images/defaultProfile.png");
$this->user->setPassword("pimwrneil");
$createNewUserCode = bin2hex(openssl_random_pseudo_bytes(16));
$hashedNewUserCode = hash('sha512', $createNewUserCode, false);
$this->user->setNewUserCode($hashedNewUserCode);
// Persist the user
$this->user->setIsActive(1);
$this->em->persist($this->user);
$this->em->flush();
return $this;
}
示例3: userIsModeratorAction
/**
* @Route("/user_is_moderator/{id}", name="admin_user_is_moderator")
* @ParamConverter("user", options={"mapping": {"id": "id"}})
*/
public function userIsModeratorAction(Request $request, User $user)
{
$user->addRole('ROLE_MODERATOR');
$em = $this->getDoctrine()->getEntityManager();
$em->flush();
return $this->redirectToRoute('admin_users_show');
}
示例4: loadUsers
/**
* @param ObjectManager $manager
*/
protected function loadUsers(ObjectManager $manager)
{
$user = new User();
$user->setEmail('learner@example.com');
$user->setFirstName('Kalle');
$user->setLastName('Anka');
$user->setEnabled(true);
$user->addRole('ROLE_COMPLETE_USER');
$user->setWantToLearn(true);
$user->setAge(35);
$user->setAbout('Sportintresserad man med 3 barn');
$user->setCategories([$this->getReference('category-1'), $this->getReference('category-2')]);
$user->setFrom('SY');
$user->setGender('M');
$user->setProfilePicture('http://api.randomuser.me/portraits/thumb/men/1.jpg');
$encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
$user->setPassword($encoder->encodePassword('asdf123', $user->getSalt()));
$manager->persist($user);
$this->addReference('user-learner', $user);
$user = new User();
$user->setEmail('fluentspeaker@example.com');
$user->setFirstName('Kal p');
$user->setLastName('Dal');
$user->setEnabled(true);
$user->setRoles(['ROLE_COMPLETE_USER', 'ROLE_ADMIN']);
$user->setWantToLearn(false);
$user->setAge(40);
$user->setAbout('Sportintresserad lärare');
$user->setCategories([$this->getReference('category-0'), $this->getReference('category-1')]);
$user->setFrom('SE');
$user->setGender('M');
$user->setProfilePicture('http://api.randomuser.me/portraits/thumb/men/2.jpg');
$encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
$user->setPassword($encoder->encodePassword('asdf123', $user->getSalt()));
$manager->persist($user);
$this->addReference('user-fluentspeaker', $user);
$user = new User();
$user->setEmail('glenn@example.com');
$user->setFirstName('Glenn');
$user->setLastName('GBG');
$user->setEnabled(true);
$user->setRoles(['ROLE_COMPLETE_USER', 'ROLE_ADMIN']);
$user->setWantToLearn(false);
$user->setAge(20);
$user->setAbout('Göteborgare');
$user->setCategories([$this->getReference('category-1'), $this->getReference('category-2')]);
$user->setFrom('SE');
$user->setGender('M');
$user->setProfilePicture('http://api.randomuser.me/portraits/thumb/men/3.jpg');
$encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
$user->setPassword($encoder->encodePassword('asdf123', $user->getSalt()));
$manager->persist($user);
$this->addReference('user-glenn', $user);
$user = new User();
$user->setEmail('incomplete@example.com');
$user->setEnabled(true);
$user->setPassword($encoder->encodePassword('asdf123', $user->getSalt()));
$manager->persist($user);
}
示例5: authenticateUser
/**
* @param User $user
* @param $role
*/
protected function authenticateUser(User $user, $role)
{
if ($role) {
$user->addRole($role);
}
$token = new UsernamePasswordToken($user, 'dummy', $providerKey = 'main');
$this->authenticateToken($token);
}
示例6: loadUser2
private function loadUser2(ObjectManager $manager)
{
$user = new User();
$user->setUsername('user2');
$user->setEmail('user2@gmail.com');
$user->setPlainPassword('user2_pass');
$user->addRole(User::ROLE_SUPER_ADMIN);
$manager->persist($user);
$manager->flush($user);
$this->addReference('user_user2', $user);
}
示例7: createUser
/**
* @Given /^que existen los siguientes usuarios:$/
*/
public function createUser(TableNode $tableNode)
{
foreach ($tableNode->getHash() as $userHash) {
$user = new User();
$user->setUsername($userHash['username']);
$user->setEmail($userHash['email']);
$user->setPlainPassword($userHash['plainPassword']);
$user->addRole($userHash['role']);
$this->getEntityManager()->persist($user);
$this->getEntityManager()->flush();
}
}
示例8: load
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
$userAdmin = new User();
$userAdmin->setUsername('admin');
$userAdmin->setEmail('admin@admin.lan');
$userAdmin->setEnabled(true);
$userAdmin->addRole('ROLE_ADMIN');
$hash = $this->container->get('security.password_encoder')->encodePassword($userAdmin, 'admin');
$userAdmin->setPassword($hash);
$manager->persist($userAdmin);
$manager->flush();
}
示例9: load
public function load(ObjectManager $manager)
{
$user = new User();
$user->setEmail('john@example.com');
$user->setUsername('john.doe');
$user->setEnabled(true);
$user->addRole('ROLE_ADMIN');
// шифрует и устанавливает пароль для пользователя,
// настройки берет из security.yml
$encoder = $this->container->get('security.password_encoder');
$password = $encoder->encodePassword($user, 'pass');
$user->setPassword($password);
$manager->persist($user);
$manager->flush();
}
示例10: load
public function load(ObjectManager $manager)
{
foreach ($this->getAllUser() as $singleUser) {
$user = new User();
$user->setUsername($singleUser['username']);
$user->setPlainPassword($singleUser['PlainPassword']);
$user->setEmail($singleUser['Email']);
$user->setEnabled(true);
$user->addRole($singleUser['roles']);
$user->setName($singleUser['name']);
$user->setIsStaff($singleUser['isStaff']);
$user->setSiteId($this->getContainer()->get('doctrine.orm.entity_manager')->getRepository('AppBundle:Site')->findBy(array('url' => $singleUser['siteId'])));
$manager->persist($user);
}
$manager->flush();
}
示例11: generateUsers
/**
* @Given /^existen los siguientes usuarios:$/
*/
public function generateUsers(TableNode $usersTable)
{
$em = $this->getEntityManager();
foreach ($usersTable->getHash() as $userHash) {
$user = $this->getEntityManager()->getRepository('AppBundle:User')->findOneByUsername($userHash['nombre']);
if (!$user) {
$user = new User();
}
$user->setUsername($userHash['nombre']);
$user->setPlainPassword($userHash['clave']);
$user->setEmail($userHash['email']);
$user->setEnabled($userHash['activado']);
$user->addRole($userHash['rol']);
$em->persist($user);
}
$em->flush();
}
示例12: create
/**
* @Route("/admin/teacher/create")
*/
public function create(Request $request)
{
$user = new User();
$user->setActive(true);
$roles = $this->getDoctrine()->getRepository('AppBundle:Role')->findOneById(2);
$user->addRole($roles);
$form = $this->createFormBuilder($user)->add('lastName', 'text', array('label' => 'Last Name:'))->add('firstName', 'text', array('label' => 'First Name:'))->add('englishName', 'text', array('label' => 'English Name:'))->add('username', 'text', array('label' => 'Login:'))->add('password', 'repeated', array('type' => 'password', 'invalid_message' => 'Password fields do not match', 'first_options' => array('label' => 'Password', 'required' => false), 'second_options' => array('label' => 'Repeat Password', 'required' => false)))->add('email', 'text', array('label' => 'Email:', 'required' => false))->add('mobilePhone', 'text', array('label' => 'Mobile Phone:', 'required' => false))->add('homePhone', 'text', array('label' => 'Home Phone:', 'required' => false))->add('roles', 'entity', array('multiple' => true, 'expanded' => true, 'class' => 'AppBundle:Role', 'choice_label' => 'name', 'label' => 'Roles: '))->add('save', 'submit', array('label' => 'Create'))->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
$user = $form->getData();
$em = $this->getDoctrine()->getManager();
if ($user->getPassword() != '') {
$user->setPassword(md5($user->getPassword()));
}
$em->persist($user);
$em->flush();
return $this->redirectToRoute($this->displayRoute);
}
return $this->render('forms/teacher.html.twig', array('form' => $form->createView()));
}
示例13: loadUsers
private function loadUsers(ObjectManager $manager)
{
$admin = $manager->getRepository('AppBundle:Role')->findOneBy(array('role' => 'ROLE_ADMIN'));
$student = $manager->getRepository('AppBundle:Role')->findOneBy(array('role' => 'ROLE_STUDENT'));
// var_dump($admin);
$passwordEncoder = $this->container->get('security.password_encoder');
$johnUser = new User();
$johnUser->setUsername('john_user');
$johnUser->setEmail('john_user@symfony.com');
$encodedPassword = $passwordEncoder->encodePassword($johnUser, 'kitten');
$johnUser->setPassword($encodedPassword);
$johnUser->addRole($student);
$manager->persist($johnUser);
$annaAdmin = new User();
$annaAdmin->setUsername('anna_admin');
$annaAdmin->setEmail('anna_admin@symfony.com');
$annaAdmin->addRole($admin);
$encodedPassword = $passwordEncoder->encodePassword($annaAdmin, 'kitten');
$annaAdmin->setPassword($encodedPassword);
$manager->persist($annaAdmin);
$manager->flush();
}
示例14: signUpAction
/**
* @Route("/signup", name="signup")
*/
public function signUpAction(Request $request)
{
$user = new User();
$em = $this->getDoctrine()->getManager();
$roleRepository = $em->getRepository('AppBundle\\Entity\\Role')->findOneBy(array('role' => 'ROLE_USER'));
$user->addRole($roleRepository);
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$emailRepository = $em->getRepository('AppBundle\\Entity\\User')->findOneBy(array('email' => $user->getEmail()));
if ($emailRepository) {
throw new \LogicException('Email already used!');
}
// Encoding the password
$plainPassword = $user->getPassword();
$encoder = $this->container->get('security.password_encoder');
$encoded = $encoder->encodePassword($user, $plainPassword);
$user->setPassword($encoded);
$em->persist($user);
$em->flush();
return $this->redirectToRoute('homepage');
}
return $this->render('default/new.html.twig', array('form' => $form->createView()));
}
示例15: addRole
/**
* {@inheritDoc}
*/
public function addRole($role)
{
$this->__initializer__ && $this->__initializer__->__invoke($this, 'addRole', [$role]);
return parent::addRole($role);
}