public function store() { $invite = new UserInvite(Input::get('email'), $this->currentOrg()); if ($invite->save()) { return $this->redirectToIndex()->withSuccess('User invited successfully'); } else { $this->view('create', ['user' => $invite->getUser()])->withErrors($invite->getErrors()); } }
public function testInviteToSpace() { $userInvite = new UserInvite(); $userInvite->user_originator_id = 1; $userInvite->space_invite_id = 2; $userInvite->email = "*****@*****.**"; $userInvite->source = UserInvite::SOURCE_INVITE; $this->assertTrue($userInvite->save()); $space = Space::model()->findByPk(2); $user = new User(); $user->username = "******"; $user->group_id = 1; $user->email = "*****@*****.**"; $this->assertTrue($user->save()); $this->assertTrue($space->isMember($user->id)); }
public function actionIndex() { $_POST = Yii::app()->input->stripClean($_POST); $assetPrefix = Yii::app()->assetManager->publish(dirname(__FILE__) . '/../resources', true, 0, defined('YII_DEBUG')); Yii::app()->clientScript->registerScriptFile($assetPrefix . '/md5.min.js'); Yii::app()->clientScript->registerScriptFile($assetPrefix . '/jdenticon-1.3.0.min.js'); $needApproval = HSetting::Get('needApproval', 'authentication_internal'); if (!Yii::app()->user->isGuest) { throw new CHttpException(401, 'Your are already logged in! - Logout first!'); } // Check for valid user invite $userInvite = UserInvite::model()->findByAttributes(array('token' => Yii::app()->request->getQuery('token'))); if (!$userInvite) { throw new CHttpException(404, 'Token not found!'); } if ($userInvite->language) { Yii::app()->setLanguage($userInvite->language); } $userModel = new User('register'); $userModel->email = $userInvite->email; $userPasswordModel = new UserPassword('newPassword'); $profileModel = $userModel->profile; $profileModel->scenario = 'register'; /////////////////////////////////////////////////////// // Generate a random first name $firstNameOptions = explode("\n", HSetting::GetText('anonAccountsFirstNameOptions')); $randomFirstName = trim(ucfirst($firstNameOptions[array_rand($firstNameOptions)])); // Generate a random last name $lastNameOptions = explode("\n", HSetting::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 = Group::model()->findAll(array('order' => 'name')); $defaultUserGroup = HSetting::Get('defaultUserGroup', 'authentication_internal'); $groupFieldType = "dropdownlist"; if ($defaultUserGroup != "") { $groupFieldType = "hidden"; } else { if (count($groupModels) == 1) { $groupFieldType = "hidden"; $defaultUserGroup = $groupModels[0]->id; } } // 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' => 'Password', 'elements' => array('username' => array('type' => 'hidden', 'class' => 'form-control', 'maxlength' => 25), 'email' => array('type' => 'hidden', 'class' => 'form-control'), 'group_id' => array('type' => $groupFieldType, 'class' => 'form-control', 'items' => CHtml::listData($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['User']->model = $userModel; $form['UserPassword']->model = $userPasswordModel; $form['Profile']->model = $profileModel; $form['IdenticonForm']->model = $identiconForm; /// ----- WE DONT WANT TO SAVE YET ------- if ($form->submitted('save') && $form->validate() && $identiconForm->validate()) { $this->forcePostRequest(); // Registe User $form['User']->model->email = $userInvite->email; $form['User']->model->language = Yii::app()->getLanguage(); if ($form['User']->model->save()) { // Save User Profile $form['Profile']->model->user_id = $form['User']->model->id; $form['Profile']->model->save(); // Save User Password $form['UserPassword']->model->user_id = $form['User']->model->id; $form['UserPassword']->model->setPassword($form['UserPassword']->model->newPassword); $form['UserPassword']->model->save(); // Autologin user if (!$needApproval) { $user = $form['User']->model; $newIdentity = new UserIdentity($user->username, ''); $newIdentity->fakeAuthenticate(); Yii::app()->user->login($newIdentity); // 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 $this->redirect(array('//dashboard/dashboard')); return; } $this->render('createAccount_success', array('form' => $form, 'needApproval' => $needApproval)); return; } } $this->render('createAccount', array('form' => $form, 'identiconForm' => $identiconForm, 'needAproval' => $needApproval)); }
/** * Before Delete of a User * */ public function beforeDelete() { // We don't allow deletion of users who owns a space - validate that foreach (SpaceMembership::GetUserSpaces($this->id) as $workspace) { if ($workspace->isSpaceOwner($this->id)) { throw new Exception("Tried to delete a user which is owner of a space!"); } } UserSetting::model()->deleteAllByAttributes(array('user_id' => $this->id)); // Disable all enabled modules foreach ($this->getAvailableModules() as $moduleId => $module) { if ($this->isModuleEnabled($moduleId)) { $this->disableModule($moduleId); } } HSearch::getInstance()->deleteModel($this); // Delete Profile Image $this->getProfileImage()->delete(); // Delete all pending invites UserInvite::model()->deleteAllByAttributes(array('user_originator_id' => $this->id)); Follow::model()->deleteAllByAttributes(array('user_id' => $this->id)); Follow::model()->deleteAllByAttributes(array('object_model' => 'User', 'object_id' => $this->id)); // Delete all group admin assignments GroupAdmin::model()->deleteAllByAttributes(array('user_id' => $this->id)); // Delete wall entries WallEntry::model()->deleteAllByAttributes(array('wall_id' => $this->wall_id)); // Deletes all content created by this user foreach (Content::model()->findAllByAttributes(array('user_id' => $this->id)) as $content) { $content->delete(); } foreach (Content::model()->findAllByAttributes(array('created_by' => $this->id)) as $content) { $content->delete(); } // Delete all passwords foreach (UserPassword::model()->findAllByAttributes(array('user_id' => $this->id)) as $password) { $password->delete(); } return parent::beforeDelete(); }
/** * Adds an member to this space. * * This can happens after an clicking "Request Membership" Link * after Approval or accepting an invite. * * @param type $userId */ public function addMember($userId) { $user = User::model()->findByPk($userId); $membership = $this->getMembership($userId); if ($membership == null) { // Add Membership $membership = new RoomMembership(); $membership->room_id = $this->getOwner()->id; $membership->user_id = $userId; $membership->status = RoomMembership::STATUS_MEMBER; $membership->invite_role = 0; $membership->admin_role = 0; $membership->share_role = 0; $userInvite = UserInvite::model()->findByAttributes(array('email' => $user->email)); if ($userInvite !== null && $userInvite->source == UserInvite::SOURCE_INVITE) { RoomInviteAcceptedNotification::fire($userInvite->user_originator_id, $user, $this->getOwner()); } } else { // User is already member if ($membership->status == RoomMembership::STATUS_MEMBER) { return true; } // User requested membership if ($membership->status == RoomMembership::STATUS_APPLICANT) { RoomApprovalRequestAcceptedNotification::fire(Yii::app()->user->id, $user, $this->getOwner()); } // User was invited if ($membership->status == RoomMembership::STATUS_INVITED) { RoomInviteAcceptedNotification::fire($membership->originator_user_id, $user, $this->getOwner()); } // Update Membership $membership->status = RoomMembership::STATUS_MEMBER; } $membership->save(); // Create Wall Activity for that $activity = new Activity(); $activity->content->room_id = $this->getOwner()->id; $activity->content->visibility = Content::VISIBILITY_PRIVATE; $activity->content->created_by = $this->getOwner()->id; $activity->created_by = $userId; $activity->type = "ActivityRoomMemberAdded"; $activity->save(); $activity->fire(); // Members can't also follow the space $this->getOwner()->unfollow($userId); // Cleanup Notifications RoomInviteNotification::remove($userId, $this->getOwner()); RoomApprovalRequestNotification::remove($userId, $this->getOwner()); }
/** * Create an account * * This action is called after e-mail validation. */ public function actionCreateAccount() { $_POST = Yii::app()->input->stripClean($_POST); $needApproval = HSetting::Get('needApproval', 'authentication_internal'); if (!Yii::app()->user->isGuest) { throw new CHttpException(401, 'Your are already logged in! - Logout first!'); } // Check for valid user invite $userInvite = UserInvite::model()->findByAttributes(array('token' => Yii::app()->request->getQuery('token'))); if (!$userInvite) { throw new CHttpException(404, 'Token not found!'); } $userModel = new User('register'); $userModel->email = $userInvite->email; $userPasswordModel = new UserPassword('newPassword'); $profileModel = $userModel->profile; $profileModel->scenario = 'register'; // Build Form Definition $definition = array(); $definition['elements'] = array(); $groupModels = Group::model()->findAll(array('order' => 'name')); $defaultUserGroup = HSetting::Get('defaultUserGroup', 'authentication_internal'); $groupFieldType = "dropdownlist"; if ($defaultUserGroup != "") { $groupFieldType = "hidden"; } else { if (count($groupModels) == 1) { $groupFieldType = "hidden"; $defaultUserGroup = $groupModels[0]->id; } } // 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' => 32), 'group_id' => array('type' => $groupFieldType, 'class' => 'form-control', 'items' => CHtml::listData($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['User']->model = $userModel; $form['UserPassword']->model = $userPasswordModel; $form['Profile']->model = $profileModel; if ($form->submitted('save') && $form->validate()) { $this->forcePostRequest(); // Registe User $form['User']->model->email = $userInvite->email; if ($form['User']->model->save()) { // Save User Profile $form['Profile']->model->user_id = $form['User']->model->id; $form['Profile']->model->save(); // Save User Password $form['UserPassword']->model->user_id = $form['User']->model->id; $form['UserPassword']->model->setPassword($form['UserPassword']->model->newPassword); $form['UserPassword']->model->save(); // Autologin user if (!$needApproval) { $user = $form['User']->model; $newIdentity = new UserIdentity($user->username, ''); $newIdentity->fakeAuthenticate(); Yii::app()->user->login($newIdentity); $this->redirect(array('//dashboard/dashboard')); return; } $this->render('createAccount_success', array('form' => $form, 'needApproval' => $needApproval)); return; } } $this->render('createAccount', array('form' => $form, 'needAproval' => $needApproval)); }
/** * Before deletion of a Space */ protected function beforeDelete() { foreach (SpaceSetting::model()->findAllByAttributes(array('space_id' => $this->id)) as $spaceSetting) { $spaceSetting->delete(); } // Disable all enabled modules foreach ($this->getAvailableModules() as $moduleId => $module) { if ($this->isModuleEnabled($moduleId)) { $this->disableModule($moduleId); } } HSearch::getInstance()->deleteModel($this); $this->getProfileImage()->delete(); // Remove all Follwers UserFollow::model()->deleteAllByAttributes(array('object_id' => $this->id, 'object_model' => 'Space')); //Delete all memberships: //First select, then delete - done to make sure that SpaceMembership::beforeDelete() is triggered $spaceMemberships = SpaceMembership::model()->findAllByAttributes(array('space_id' => $this->id)); foreach ($spaceMemberships as $spaceMembership) { $spaceMembership->delete(); } UserInvite::model()->deleteAllByAttributes(array('space_invite_id' => $this->id)); // Delete all content objects of this space foreach (Content::model()->findAllByAttributes(array('space_id' => $this->id)) as $content) { $content->delete(); } // When this workspace is used in a group as default workspace, delete the link foreach (Group::model()->findAllByAttributes(array('space_id' => $this->id)) as $group) { $group->space_id = ""; $group->save(); } Wall::model()->deleteAllByAttributes(array('id' => $this->wall_id)); return parent::beforeDelete(); }
/** * Invites a not registered member to this space * * @param type $email * @param type $originatorUserId */ public function inviteMemberByEMail($email, $originatorUserId) { // Invalid E-Mail $validator = new CEmailValidator(); if (!$validator->validateValue($email)) { return false; } // User already registered $user = User::model()->findByAttributes(array('email' => $email)); if ($user != null) { return false; } $userInvite = UserInvite::model()->findByAttributes(array('email' => $email)); // No invite yet if ($userInvite == null) { // Invite EXTERNAL user $userInvite = new UserInvite(); $userInvite->email = $email; $userInvite->source = UserInvite::SOURCE_INVITE; $userInvite->user_originator_id = $originatorUserId; $userInvite->space_invite_id = $this->getOwner()->id; $userInvite->save(); $userInvite->sendInviteMail(); // There is a pending registration // Steal it und send mail again // Unfortunately there a no multiple workspace invites supported // so we take the last one } else { $userInvite->user_originator_id = $originatorUserId; $userInvite->space_invite_id = $this->getOwner()->id; $userInvite->save(); $userInvite->sendInviteMail(); } }
/** * Before deletion of a Room */ protected function beforeDelete() { foreach (RoomSetting::model()->findAllByAttributes(array('room_id' => $this->id)) as $roomSetting) { $roomSetting->delete(); } // Disable all enabled modules foreach ($this->getAvailableModules() as $moduleId => $module) { if ($this->isModuleEnabled($moduleId)) { $this->disableModule($moduleId); } } Yii::app()->search->delete($this); $this->getProfileImage()->delete(); // Remove all Follwers //UserFollow::model()->deleteAllByAttributes(array('object_id' => $this->id, 'object_model' => 'Room')); //Delete all memberships: //First select, then delete - done to make sure that RoomsMembership::beforeDelete() is triggered $roomMemberships = RoomMembership::model()->findAllByAttributes(array('room_id' => $this->id)); foreach ($roomMemberships as $roomMembership) { $roomMembership->delete(); } UserInvite::model()->deleteAllByAttributes(array('room_invite_id' => $this->id)); // Delete all content objects of this room foreach (Content::model()->findAllByAttributes(array('room_id' => $this->id)) as $content) { $content->delete(); } // When this room is used in a group as default room, delete the link foreach (Group::model()->findAllByAttributes(array('room_id' => $this->id)) as $group) { $group->room_id = ""; $group->save(); } Wall::model()->deleteAllByAttributes(array('id' => $this->wall_id)); return parent::beforeDelete(); }
public function actionMail() { $userInvite = UserInvite::model()->findByAttributes(array('email' => Yii::app()->request->getQuery('email'))); if ($userInvite === null) { $userInvite = new UserInvite(); } else { $update = Yii::app()->db->createCommand()->update('user_invite', array('created_at' => new CDbExpression('NOW()'), 'updated_at' => new CDbExpression('NOW()')), 'email=:email', array(':email' => Yii::app()->request->getQuery('email'))); $userInvite->sendInviteMail(); } $this->render("mail"); }