Example #1
0
 /**
  * Tests if the user group can save and return a id.
  */
 public function testId()
 {
     $group = new Group();
     $group->setId(3);
     $this->assertEquals(3, $group->getId(), 'The group id did not save correctly.');
     $this->assertTrue(is_int($group->getId()), 'The group id was not returned as an Integer.');
 }
Example #2
0
 /**
  * Create not privileged user
  * @param $username
  * @param $password
  * @param $email
  * @param array $extra
  * @return array|\Modules\User\Models\User Errors or created model
  */
 public function createUser($username, $password, $email = null, array $extra = [])
 {
     /** @var \Modules\User\PasswordHasher\IPasswordHasher $hasher */
     $auth = Mindy::app()->auth;
     $hasher = $auth->getPasswordHasher(isset($extra['hash_type']) ? $extra['hash_type'] : $this->defaultPasswordHasher);
     $model = $this->getModel();
     $model->setAttributes(array_merge(['username' => $username, 'email' => $email, 'password' => $hasher->hashPassword($password), 'activation_key' => $this->generateActivationKey()], $extra));
     if ($model->isValid() && $model->save()) {
         $groups = Group::objects()->filter(['is_default' => true])->all();
         foreach ($groups as $group) {
             $model->groups->link($group);
         }
         $permission = Permission::objects()->filter(['is_default' => true])->all();
         foreach ($permission as $perm) {
             $model->permissions->link($perm);
         }
         $eventManager = $this->getEventManager();
         $module = Mindy::app()->getModule('User');
         if ($module->sendUserCreateMail) {
             $eventManager->send($model, 'createUser', $model);
         } else {
             if ($module->sendUserCreateRawMail) {
                 $eventManager->send($model, 'createRawUser', $model, $password);
             }
         }
     }
     return $model;
 }
Example #3
0
 /**
  * Shows a form to create or edit a new user.
  */
 public function treatAction()
 {
     $this->getLayout()->getAdminHmenu()->add($this->getTranslator()->trans('menuUser'), array('action' => 'index'))->add($this->getTranslator()->trans('editUser'), array('action' => 'treat'));
     $userMapper = new UserMapper();
     if ($this->getRequest()->isPost()) {
         $userData = $this->getRequest()->getPost('user');
         if (!empty($userData['password'])) {
             $userData['password'] = (new PasswordService())->hash($userData['password']);
         }
         $user = $userMapper->loadFromArray($userData);
         if (!empty($userData['groups'])) {
             foreach ($userData['groups'] as $groupId) {
                 $group = new GroupModel();
                 $group->setId($groupId);
                 $user->addGroup($group);
             }
         }
         $date = new \Ilch\Date();
         $user->setDateCreated($date);
         $userId = $userMapper->save($user);
         if (!empty($userId) && empty($userData['id'])) {
             $this->addMessage('newUserMsg');
         }
     }
     if (empty($userId)) {
         $userId = $this->getRequest()->getParam('id');
     }
     if ($userMapper->userWithIdExists($userId)) {
         $user = $userMapper->getUserById($userId);
     } else {
         $user = new UserModel();
     }
     $groupMapper = new GroupMapper();
     $this->getView()->set('user', $user);
     $this->getView()->set('groupList', $groupMapper->getGroupList());
 }
Example #4
0
 public function testCreateUserWithDefaultGroup()
 {
     $group = new Group(['name' => 'test', 'is_default' => true]);
     $this->assertTrue($group->save());
     $user = User::objects()->createUser('foo', 'bar', '*****@*****.**');
     $this->assertEquals(1, $user->groups->count());
 }
Example #5
0
 public static function getFields()
 {
     return ['user_group' => ['class' => ForeignField::className(), 'modelClass' => Group::className(), 'verboseName' => UserModule::t("Group")], 'permission' => ['class' => ForeignField::className(), 'modelClass' => Permission::className(), 'verboseName' => UserModule::t("Permission")]];
 }
Example #6
0
 /**
  * Inserts or updates a user group model into the database.
  *
  * @param GroupModel $group
  */
 public function save(GroupModel $group)
 {
     $fields = array();
     $name = $group->getName();
     if (!empty($name)) {
         $fields['name'] = $group->getName();
     }
     $groupId = (int) $this->db()->select('id', 'groups', array('id' => $group->getId()))->execute()->fetchCell();
     if ($groupId) {
         /*
          * Group does exist already, update.
          */
         $this->db()->update('groups')->values($fields)->where(array('id' => $groupId))->execute();
     } else {
         /*
          * Group does not exist yet, insert.
          */
         $groupId = $this->db()->insert('groups')->values($fields)->execute();
     }
     return $groupId;
 }
Example #7
0
 /**
  * Tests if the access for a user can be returned.
  */
 public function testHasAccess()
 {
     $group = new Group();
     $group->setId(3);
     $group->setName('Testgroup');
     $user = new User();
     $user->setId(123);
     $user->addGroup($group);
     $dbMock = $this->getMock('Ilch_Database', array('queryCell'));
     $dbMock->expects($this->once())->method('queryCell')->with($this->logicalAnd($this->stringContains('FROM [prefix]_groups_access'), $this->stringContains('INNER JOIN `[prefix]_modules`'), $this->stringContains('user')))->will($this->returnValue('0'));
     Registry::remove('db');
     Registry::set('db', $dbMock);
     $this->assertEquals(0, $user->hasAccess('module_user'));
 }