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; }
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; }
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 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)); }
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)); }