/**
  * Displays the login page
  */
 public function actionLogin()
 {
     // If user is already logged in, redirect him to the dashboard
     if (!Yii::$app->user->isGuest) {
         return $this->goBack();
     }
     // Login Form Handling
     $login = new Login();
     if ($login->load(Yii::$app->request->post()) && $login->validate()) {
         return $this->onAuthSuccess($login->authClient);
     }
     // Self Invite
     $invite = new Invite();
     $invite->scenario = 'invite';
     if ($invite->load(Yii::$app->request->post()) && $invite->selfInvite()) {
         if (Yii::$app->request->getIsAjax()) {
             return $this->render('register_success_modal', ['model' => $invite]);
         } else {
             return $this->render('register_success', ['model' => $invite]);
         }
     }
     if (Yii::$app->request->getIsAjax()) {
         return $this->renderAjax('login_modal', array('model' => $login, 'invite' => $invite, 'canRegister' => $invite->allowSelfInvite()));
     }
     return $this->render('login', array('model' => $login, 'invite' => $invite, 'canRegister' => $invite->allowSelfInvite()));
 }
 /**
  * Creates and sends an e-mail invite
  * 
  * @param email $email
  */
 protected function createInvite($email)
 {
     $userInvite = new Invite();
     $userInvite->email = $email;
     $userInvite->source = Invite::SOURCE_INVITE;
     $userInvite->user_originator_id = Yii::$app->user->getIdentity()->id;
     $userInvite->save();
     $userInvite->sendInviteMail();
 }
Beispiel #3
0
 public function testInviteToSpace()
 {
     Yii::$app->getModule('user')->settings->set('auth.needApproval', 0);
     $invite = new Invite();
     $invite->user_originator_id = 1;
     $invite->space_invite_id = 2;
     $invite->email = "*****@*****.**";
     $invite->source = Invite::SOURCE_INVITE;
     $this->assertTrue($invite->save());
     $space = Space::findOne(['id' => 2]);
     $user = new User();
     $user->username = "******";
     $user->email = "*****@*****.**";
     $this->assertTrue($user->save());
     $this->assertTrue($space->isMember($user->id));
 }
Beispiel #4
0
 public function testInviteToSpace()
 {
     \humhub\models\Setting::Set('needApproval', 0, 'authentication_internal');
     $invite = new Invite();
     $invite->user_originator_id = 1;
     $invite->space_invite_id = 2;
     $invite->email = "*****@*****.**";
     $invite->source = Invite::SOURCE_INVITE;
     $this->assertTrue($invite->save());
     $space = Space::findOne(['id' => 2]);
     $user = new User();
     $user->username = "******";
     $user->group_id = 1;
     $user->email = "*****@*****.**";
     $this->assertTrue($user->save());
     $this->assertTrue($space->isMember($user->id));
 }
Beispiel #5
0
 /**
  * Displays the login page
  */
 public function actionLogin()
 {
     // If user is already logged in, redirect him to the dashboard
     if (!Yii::$app->user->isGuest) {
         $this->redirect(Yii::$app->user->returnUrl);
     }
     // Show/Allow Anonymous Registration
     $loginModel = new \humhub\modules\user\models\forms\AccountLogin();
     if ($loginModel->load(Yii::$app->request->post()) && $loginModel->login()) {
         if (Yii::$app->request->getIsAjax()) {
             return $this->htmlRedirect(Yii::$app->user->returnUrl);
         } else {
             return $this->redirect(Yii::$app->user->returnUrl);
         }
     }
     $loginModel->password = "";
     $canRegister = \humhub\models\Setting::Get('anonymousRegistration', 'authentication_internal');
     $registerModel = new \humhub\modules\user\models\forms\AccountRegister();
     if ($canRegister) {
         if ($registerModel->load(Yii::$app->request->post()) && $registerModel->validate()) {
             $invite = \humhub\modules\user\models\Invite::findOne(['email' => $registerModel->email]);
             if ($invite === null) {
                 $invite = new \humhub\modules\user\models\Invite();
             }
             $invite->email = $registerModel->email;
             $invite->source = \humhub\modules\user\models\Invite::SOURCE_SELF;
             $invite->language = Yii::$app->language;
             $invite->save();
             $invite->sendInviteMail();
             if (Yii::$app->request->getIsAjax()) {
                 return $this->render('register_success_modal', ['model' => $registerModel]);
             } else {
                 return $this->render('register_success', ['model' => $registerModel]);
             }
         }
     }
     if (Yii::$app->request->getIsAjax()) {
         return $this->renderAjax('login_modal', array('model' => $loginModel, 'registerModel' => $registerModel, 'canRegister' => $canRegister));
     } else {
         return $this->render('login', array('model' => $loginModel, 'registerModel' => $registerModel, 'canRegister' => $canRegister));
     }
 }
 protected function handleInviteRegistration($inviteToken, Registration $form)
 {
     $userInvite = Invite::findOne(['token' => $inviteToken]);
     if (!$userInvite) {
         throw new HttpException(404, 'Invalid registration token!');
     }
     if ($userInvite->language) {
         Yii::$app->language = $userInvite->language;
     }
     $form->getUser()->email = $userInvite->email;
 }
Beispiel #7
0
 public function selfInvite()
 {
     $this->source = self::SOURCE_SELF;
     $this->language = Yii::$app->language;
     // Delete existing invite for e-mail - but reuse token
     $existingInvite = Invite::findOne(['email' => $this->email]);
     if ($existingInvite !== null) {
         $this->token = $existingInvite->token;
         $existingInvite->delete();
     }
     if ($this->allowSelfInvite() && $this->validate() && $this->save()) {
         $this->sendInviteMail();
         return true;
     }
     return false;
 }
Beispiel #8
0
 public function setUpApproved()
 {
     $userInvite = Invite::findOne(['email' => $this->email]);
     if ($userInvite !== null) {
         // User was invited to a space
         if ($userInvite->source == Invite::SOURCE_INVITE) {
             $space = \humhub\modules\space\models\Space::findOne(['id' => $userInvite->space_invite_id]);
             if ($space != null) {
                 $space->addMember($this->id);
             }
         }
         // Delete/Cleanup Invite Entry
         $userInvite->delete();
     }
     // Auto Assign User to the Group Space
     /* $group = Group::findOne(['id' => $this->group_id]);
        if ($group != null && $group->space_id != "") {
        $space = \humhub\modules\space\models\Space::findOne(['id' => $group->space_id]);
        if ($space !== null) {
        $space->addMember($this->id);
        }
        } */
     // Auto Add User to the default spaces
     foreach (\humhub\modules\space\models\Space::findAll(['auto_add_new_members' => 1]) as $space) {
         $space->addMember($this->id);
     }
 }
Beispiel #9
0
 /**
  * Create an account
  *
  * This action is called after e-mail validation.
  */
 public function actionCreateAccount()
 {
     $needApproval = \humhub\models\Setting::Get('needApproval', 'authentication_internal');
     if (!Yii::$app->user->isGuest) {
         throw new HttpException(401, 'Your are already logged in! - Logout first!');
     }
     $userInvite = Invite::findOne(['token' => Yii::$app->request->get('token')]);
     if (!$userInvite) {
         throw new HttpException(404, 'Token not found!');
     }
     if ($userInvite->language) {
         Yii::$app->language = $userInvite->language;
     }
     $userModel = new User();
     $userModel->scenario = 'registration';
     $userModel->email = $userInvite->email;
     $userPasswordModel = new Password();
     $userPasswordModel->scenario = 'registration';
     $profileModel = $userModel->profile;
     $profileModel->scenario = 'registration';
     // Build Form Definition
     $definition = array();
     $definition['elements'] = array();
     $groupModels = \humhub\modules\user\models\Group::find()->orderBy('name ASC')->all();
     $defaultUserGroup = \humhub\models\Setting::Get('defaultUserGroup', 'authentication_internal');
     $groupFieldType = "dropdownlist";
     if ($defaultUserGroup != "") {
         $groupFieldType = "hidden";
     } else {
         if (count($groupModels) == 1) {
             $groupFieldType = "hidden";
             $defaultUserGroup = $groupModels[0]->id;
         }
     }
     if ($groupFieldType == 'hidden') {
         $userModel->group_id = $defaultUserGroup;
     }
     // Add User Form
     $definition['elements']['User'] = array('type' => 'form', 'title' => Yii::t('UserModule.controllers_AuthController', 'Account'), 'elements' => array('username' => array('type' => 'text', 'class' => 'form-control', 'maxlength' => 25), 'group_id' => array('type' => $groupFieldType, 'class' => 'form-control', 'items' => \yii\helpers\ArrayHelper::map($groupModels, 'id', 'name'), 'value' => $defaultUserGroup)));
     // Add User Password Form
     $definition['elements']['UserPassword'] = array('type' => 'form', 'elements' => array('newPassword' => array('type' => 'password', 'class' => 'form-control', 'maxlength' => 255), 'newPasswordConfirm' => array('type' => 'password', 'class' => 'form-control', 'maxlength' => 255)));
     // Add Profile Form
     $definition['elements']['Profile'] = array_merge(array('type' => 'form'), $profileModel->getFormDefinition());
     // Get Form Definition
     $definition['buttons'] = array('save' => array('type' => 'submit', 'class' => 'btn btn-primary', 'label' => Yii::t('UserModule.controllers_AuthController', 'Create account')));
     $form = new HForm($definition);
     $form->models['User'] = $userModel;
     $form->models['UserPassword'] = $userPasswordModel;
     $form->models['Profile'] = $profileModel;
     if ($form->submitted('save') && $form->validate()) {
         $this->forcePostRequest();
         // Registe User
         $form->models['User']->email = $userInvite->email;
         $form->models['User']->language = Yii::$app->language;
         if ($form->models['User']->save()) {
             // Save User Profile
             $form->models['Profile']->user_id = $form->models['User']->id;
             $form->models['Profile']->save();
             // Save User Password
             $form->models['UserPassword']->user_id = $form->models['User']->id;
             $form->models['UserPassword']->setPassword($form->models['UserPassword']->newPassword);
             $form->models['UserPassword']->save();
             // Autologin user
             if (!$needApproval) {
                 Yii::$app->user->switchIdentity($form->models['User']);
                 return $this->redirect(Url::to(['/dashboard/dashboard']));
             }
             return $this->render('createAccount_success', array('form' => $form, 'needApproval' => $needApproval));
         }
     }
     return $this->render('createAccount', array('hForm' => $form, 'needAproval' => $needApproval));
 }
Beispiel #10
0
 /**
  * @inheritdoc
  */
 public function beforeDelete()
 {
     foreach (Setting::findAll(['space_id' => $this->id]) as $spaceSetting) {
         $spaceSetting->delete();
     }
     foreach ($this->getAvailableModules() as $moduleId => $module) {
         if ($this->isModuleEnabled($moduleId)) {
             $this->disableModule($moduleId);
         }
     }
     Yii::$app->search->delete($this);
     $this->getProfileImage()->delete();
     \humhub\modules\user\models\Follow::deleteAll(['object_id' => $this->id, 'object_model' => 'Space']);
     foreach (Membership::findAll(['space_id' => $this->id]) as $spaceMembership) {
         $spaceMembership->delete();
     }
     \humhub\modules\user\models\Invite::deleteAll(['space_invite_id' => $this->id]);
     // When this workspace is used in a group as default workspace, delete the link
     foreach (\humhub\modules\user\models\Group::findAll(['space_id' => $this->id]) as $group) {
         $group->space_id = "";
         $group->save();
     }
     return parent::beforeDelete();
 }
Beispiel #11
0
 public function setUpApproved()
 {
     $userInvite = Invite::findOne(['email' => $this->email]);
     if ($userInvite !== null) {
         // User was invited to a space
         if ($userInvite->source == Invite::SOURCE_INVITE) {
             $space = \humhub\modules\space\models\Space::findOne(['id' => $userInvite->space_invite_id]);
             if ($space != null) {
                 $space->addMember($this->id);
             }
         }
         // Delete/Cleanup Invite Entry
         $userInvite->delete();
     }
     // Auto Assign User to the Group Space
     $group = Group::findOne(['id' => $this->group_id]);
     if ($group != null && $group->space_id != "") {
         $space = \humhub\modules\space\models\Space::findOne(['id' => $group->space_id]);
         if ($space !== null) {
             $space->addMember($this->id);
         }
     }
     // Auto Add User to the default spaces
     foreach (\humhub\modules\space\models\Space::findAll(['auto_add_new_members' => 1]) as $space) {
         $space->addMember($this->id);
     }
     // Create new wall record for this user
     $wall = new \humhub\modules\content\models\Wall();
     $wall->object_model = $this->className();
     $wall->object_id = $this->id;
     $wall->save();
     $this->wall_id = $wall->id;
     $this->update(false, ['wall_id']);
 }
 /**
  * Adds an member to this space.
  *
  * This can happens after an clicking "Request Membership" Link
  * after Approval or accepting an invite.
  * Reputation id is 1 for joining
  *
  * Reputation is 5 for those who invited them
  * @param type $userId
  */
 public function addMember($userId)
 {
     $user = User::findOne(['id' => $userId]);
     $membership = $this->getMembership($userId);
     if ($membership == null) {
         // Add Membership
         $reputation_id = 1;
         $membership = new Membership();
         $membership->space_id = $this->owner->id;
         $membership->user_id = $userId;
         $membership->status = Membership::STATUS_MEMBER;
         $membership->invite_role = 0;
         $membership->admin_role = 0;
         $membership->share_role = 0;
         $userInvite = Invite::findOne(['email' => $user->email]);
         if ($userInvite !== null && $userInvite->source == Invite::SOURCE_INVITE) {
             $reputation_id = 5;
             $notification = new \humhub\modules\space\notifications\InviteAccepted();
             $notification->originator = $user;
             $notification->source = $this->owner;
             $notification->send(User::findOne(['id' => $userInvite->user_originator_id]));
             ReputationHistory::addReputation($membership->originator_user_id, $reputation_id);
         }
     } else {
         // User is already member
         if ($membership->status == Membership::STATUS_MEMBER) {
             return true;
         }
         // User requested membership
         if ($membership->status == Membership::STATUS_APPLICANT) {
             $notification = new \humhub\modules\space\notifications\ApprovalRequestAccepted();
             $notification->source = $this->owner;
             $notification->originator = Yii::$app->user->getIdentity();
             $notification->send($user);
         }
         // User was invited
         if ($membership->status == Membership::STATUS_INVITED) {
             $notification = new \humhub\modules\space\notifications\InviteAccepted();
             $notification->source = $this->owner;
             $notification->originator = $user;
             $notification->send(User::findOne(['id' => $membership->originator_user_id]));
         }
         // Update Membership
         $membership->status = Membership::STATUS_MEMBER;
     }
     $membership->save();
     ReputationHistory::addReputation($userId, $reputation_id);
     $activity = new \humhub\modules\space\activities\MemberAdded();
     $activity->source = $this->owner;
     $activity->originator = $user;
     $activity->create();
     // Members can't also follow the space
     $this->owner->unfollow($userId);
     // Delete invite notification for this user
     $notificationInvite = new \humhub\modules\space\notifications\Invite();
     $notificationInvite->source = $this->owner;
     $notificationInvite->delete($user);
     // Delete pending approval request notifications for this user
     $notificationApprovalRequest = new \humhub\modules\space\notifications\ApprovalRequest();
     $notificationApprovalRequest->source = $this->owner;
     $notificationApprovalRequest->originator = $user;
     $notificationApprovalRequest->delete();
 }
 public function actionIndex()
 {
     $needApproval = Setting::Get('needApproval', 'authentication_internal');
     if (!Yii::$app->user->isGuest) {
         throw new HttpException(401, 'Your are already logged in! - Logout first!');
     }
     $userInvite = Invite::findOne(['token' => Yii::$app->request->get('token')]);
     if (!$userInvite) {
         throw new HttpException(404, 'Token not found!');
     }
     if ($userInvite->language) {
         Yii::$app->language = $userInvite->language;
     }
     $userModel = new User();
     $userModel->scenario = 'registration';
     $userModel->email = $userInvite->email;
     $userPasswordModel = new Password();
     $userPasswordModel->scenario = 'registration';
     $profileModel = $userModel->profile;
     $profileModel->scenario = 'registration';
     ///////////////////////////////////////////////////////
     // Generate a random first name
     $firstNameOptions = explode("\n", Setting::GetText('anonAccountsFirstNameOptions'));
     $randomFirstName = trim(ucfirst($firstNameOptions[array_rand($firstNameOptions)]));
     // Generate a random last name
     $lastNameOptions = explode("\n", Setting::GetText('anonAccountsLastNameOptions'));
     $randomLastName = trim(ucfirst($lastNameOptions[array_rand($lastNameOptions)]));
     // Pre-set the random first and last name
     $profileModel->lastname = $randomLastName;
     $profileModel->firstname = $randomFirstName;
     // Make the username from the first and lastnames (only first 25 chars)
     $userModel->username = substr(str_replace(" ", "_", strtolower($profileModel->firstname . "_" . $profileModel->lastname)), 0, 25);
     ///////////////////////////////////////////////////////
     // Build Form Definition
     $definition = array();
     $definition['elements'] = array();
     $groupModels = \humhub\modules\user\models\Group::find()->orderBy('name ASC')->all();
     $defaultUserGroup = \humhub\models\Setting::Get('defaultUserGroup', 'authentication_internal');
     $groupFieldType = "dropdownlist";
     if ($defaultUserGroup != "") {
         $groupFieldType = "hidden";
     } else {
         if (count($groupModels) == 1) {
             $groupFieldType = "hidden";
             $defaultUserGroup = $groupModels[0]->id;
         }
     }
     if ($groupFieldType == 'hidden') {
         $userModel->group_id = $defaultUserGroup;
     }
     // Add Identicon Form
     $identiconForm = new IdenticonForm();
     $definition['elements']['IdenticonForm'] = array('type' => 'form', 'elements' => array('image' => array('type' => 'hidden', 'class' => 'form-control', 'id' => 'image')));
     // Add Profile Form
     $definition['elements']['Profile'] = array_merge(array('type' => 'form'), $profileModel->getFormDefinition());
     // Add User Form
     $definition['elements']['User'] = array('type' => 'form', 'title' => Yii::t('UserModule.controllers_AuthController', 'Account'), 'elements' => array('username' => array('type' => 'hidden', 'class' => 'form-control', 'maxlength' => 25), 'group_id' => array('type' => $groupFieldType, 'class' => 'form-control', 'items' => \yii\helpers\ArrayHelper::map($groupModels, 'id', 'name'), 'value' => $defaultUserGroup)));
     // Add User Password Form
     $definition['elements']['UserPassword'] = array('type' => 'form', 'elements' => array('newPassword' => array('type' => 'password', 'class' => 'form-control', 'maxlength' => 255), 'newPasswordConfirm' => array('type' => 'password', 'class' => 'form-control', 'maxlength' => 255)));
     // Get Form Definition
     $definition['buttons'] = array('save' => array('type' => 'submit', 'class' => 'btn btn-primary', 'label' => Yii::t('UserModule.controllers_AuthController', 'Create account')));
     $form = new HForm($definition);
     $form->models['User'] = $userModel;
     $form->models['UserPassword'] = $userPasswordModel;
     $form->models['Profile'] = $profileModel;
     $form->models['IdenticonForm'] = $identiconForm;
     if ($form->submitted('save') && $form->validate() && $identiconForm->validate()) {
         $this->forcePostRequest();
         // Registe User
         $form->models['User']->email = $userInvite->email;
         $form->models['User']->language = Yii::$app->language;
         if ($form->models['User']->save()) {
             // Save User Profile
             $form->models['Profile']->user_id = $form->models['User']->id;
             $form->models['Profile']->save();
             // Save User Password
             $form->models['UserPassword']->user_id = $form->models['User']->id;
             $form->models['UserPassword']->setPassword($form->models['UserPassword']->newPassword);
             $form->models['UserPassword']->save();
             // Autologin user
             if (!$needApproval) {
                 $user = $form->models['User'];
                 Yii::$app->user->login($user);
                 // Prepend Data URI scheme (stripped out for safety)
                 $identiconForm->image = str_replace("[removed]", "data:image/png;base64,", $identiconForm->image);
                 // Upload new Profile Picture for user
                 $this->uploadProfilePicture(Yii::$app->user->guid, $identiconForm->image);
                 // Redirect to dashboard
                 return $this->redirect(Url::to(['/dashboard/dashboard']));
             }
             return $this->render('createAccount_success', array('form' => $form, 'needApproval' => $needApproval));
         }
     }
     return $this->render('createAccount', array('hForm' => $form, 'needAproval' => $needApproval));
 }