/** * Edits a user * * @return type */ public function actionEdit() { $user = UserEditForm::findOne(['id' => Yii::$app->request->get('id')]); $user->initGroupSelection(); if ($user == null) { throw new \yii\web\HttpException(404, Yii::t('AdminModule.controllers_UserController', 'User not found!')); } $user->scenario = 'editAdmin'; $user->profile->scenario = 'editAdmin'; $profile = $user->profile; // Build Form Definition $definition = []; $definition['elements'] = []; // Add User Form $definition['elements']['User'] = ['type' => 'form', 'title' => 'Account', 'elements' => ['username' => ['type' => 'text', 'class' => 'form-control', 'maxlength' => 25], 'email' => ['type' => 'text', 'class' => 'form-control', 'maxlength' => 100], 'groupSelection' => ['id' => 'user_edit_groups', 'type' => 'multiselectdropdown', 'items' => UserEditForm::getGroupItems()], 'status' => ['type' => 'dropdownlist', 'class' => 'form-control', 'items' => [User::STATUS_ENABLED => Yii::t('AdminModule.controllers_UserController', 'Enabled'), User::STATUS_DISABLED => Yii::t('AdminModule.controllers_UserController', 'Disabled'), User::STATUS_NEED_APPROVAL => Yii::t('AdminModule.controllers_UserController', 'Unapproved')]]]]; // Add Profile Form $definition['elements']['Profile'] = array_merge(array('type' => 'form'), $profile->getFormDefinition()); // Get Form Definition $definition['buttons'] = array('save' => array('type' => 'submit', 'label' => Yii::t('AdminModule.controllers_UserController', 'Save'), 'class' => 'btn btn-primary'), 'become' => array('type' => 'submit', 'label' => Yii::t('AdminModule.controllers_UserController', 'Become this user'), 'class' => 'btn btn-danger'), 'delete' => array('type' => 'submit', 'label' => Yii::t('AdminModule.controllers_UserController', 'Delete'), 'class' => 'btn btn-danger')); $form = new HForm($definition); $form->models['User'] = $user; $form->models['Profile'] = $profile; if ($form->submitted('save') && $form->validate()) { if ($form->save()) { return $this->redirect(['/admin/user']); } } // This feature is used primary for testing, maybe remove this in future if ($form->submitted('become')) { Yii::$app->user->switchIdentity($form->models['User']); return $this->redirect(Url::home()); } if ($form->submitted('delete')) { return $this->redirect(['/admin/user/delete', 'id' => $user->id]); } return $this->render('edit', array('hForm' => $form, 'user' => $user)); }
/** * 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 actionAdd() { $userModel = new User(); $userModel->scenario = 'registration'; $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), 'email' => array('type' => 'text', 'class' => 'form-control', 'maxlength' => 100), '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(); $form->models['User']->status = User::STATUS_ENABLED; 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(); return $this->redirect(Url::to(['index'])); } } return $this->render('add', array('hForm' => $form)); }
/** * @inheritdoc */ public function validate() { // Ensure Models $this->setModels(); // Remove optional group assignment before validation // GroupUser assignment is optional and will be validated on save $groupUser = $this->models['GroupUser']; unset($this->models['GroupUser']); $status = parent::validate(); $this->models['GroupUser'] = $groupUser; return $status; }
/** * Edit a karma record */ public function actionEdit() { $id = (int) Yii::$app->request->get('id'); $user = User::findOne(['id' => $id]); $karma = Karma::findOne(['id' => $id]); if ($karma == null) { throw new \yii\web\HttpException(404, "Karma record not found!"); } // Build Form Definition $definition = array(); $definition['elements'] = array(); // Define Form Eleements $definition['elements']['Karma'] = array('type' => 'form', 'title' => 'Karma', 'elements' => array('name' => array('type' => 'text', 'class' => 'form-control', 'maxlength' => 25), 'points' => array('type' => 'text', 'class' => 'form-control', 'maxlength' => 10), 'description' => array('type' => 'text', 'class' => 'form-control', 'maxlength' => 1000))); // Get Form Definition $definition['buttons'] = array('save' => array('type' => 'submit', 'label' => 'Save', 'class' => 'btn btn-primary'), 'delete' => array('type' => 'submit', 'label' => 'Delete', 'class' => 'btn btn-danger')); $form = new HForm($definition); $form->models['Karma'] = $karma; if ($form->submitted('save') && $form->validate()) { if ($form->save()) { return $this->redirect(Url::toRoute(['edit', 'id' => $karma->id])); } } if ($form->submitted('delete')) { return $this->redirect(Url::toRoute(['delete', 'id' => $karma->id])); } return $this->render('edit', array('hForm' => $form)); }
public function actionEditField() { $id = (int) Yii::$app->request->get('id'); // Get Base Field $field = ProfileField::findOne(['id' => $id]); if ($field == null) { $field = new ProfileField(); } // Get all Available Field Class Instances, also bind current profilefield to the type $profileFieldTypes = new BaseType(); $fieldTypes = $profileFieldTypes->getTypeInstances($field); // Build Form Definition $definition = array(); $definition['elements'] = array(); // Add all sub forms $definition['elements'] = array_merge($definition['elements'], $field->getFormDefinition()); foreach ($fieldTypes as $fieldType) { $definition['elements'] = array_merge($definition['elements'], $fieldType->getFormDefinition()); } // Add Form Buttons $definition['buttons'] = array('save' => array('type' => 'submit', 'label' => Yii::t('AdminModule.controllers_UserprofileController', 'Save'), 'class' => 'btn btn-primary')); if (!$field->isNewRecord && !$field->is_system) { $definition['buttons']['delete'] = array('type' => 'submit', 'label' => Yii::t('AdminModule.controllers_UserprofileController', 'Delete'), 'class' => 'btn btn-danger pull-right'); } // Create Form Instance $form = new HForm($definition); // Add used models to the CForm, so we can validate it $form->models['ProfileField'] = $field; foreach ($fieldTypes as $fieldType) { $form->models[get_class($fieldType)] = $fieldType; } // Form Submitted? if ($form->submitted('save') && $form->validate()) { // Use ProfileField Instance from Form with new Values $field = $form->models['ProfileField']; $fieldType = $form->models[$field->field_type_class]; if ($field->save() && $fieldType->save()) { return $this->redirect(Url::to(['/admin/user-profile'])); } } if ($form->submitted('delete')) { $field->delete(); return $this->redirect(Url::to(['/admin/user-profile'])); } return $this->render('editField', array('hForm' => $form, 'field' => $field)); }
/** * @inheritdoc */ public function validate() { // Ensure Models $this->setModels(); return parent::validate(); }
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)); }