'trim' ], [ 'username', 'required' ], [ 'username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.' ], [ 'username', 'string', 'min' => 2, 'max' => 255 ], [ 'email', 'filter', 'filter' => 'trim' ], [ 'email', 'required' ], [ 'email', 'email' ], [ 'email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.' ], [ 'password', 'required' ], [ 'password', 'string', 'min' => 6 ], [ ['name', 'firstName', 'institution', 'streetPOBox', 'postalCode', 'locationCity', 'country', 'emailSecond', 'passwordSecond'], 'required' ], [ 'passwordSecond', 'compare', 'compareAttribute'=>'password', 'message' => 'Verify the spelling of your password' ], [ 'emailSecond', 'compare', 'compareAttribute'=>'email', 'operator' => '===', 'message' => 'Verify the spelling of your email address' ], //In order to recieve user input, attributes must have at least one rule. "safe" is default rule. //Possibilty to store Cites-codes in database an perfrom inline validation??? [ ['form_of_address', 'title', 'division', 'deliveryInstitution', 'deliveryDivision', 'deliveryStreet', 'deliveryPostalCode', 'deliveryLocation', 'deliveryCountry', 'citesCode'], 'safe' ] ]; } /** * (non-PHPdoc) * @see \yii\base\Model::attributeLabels() * @overrides * */ public function attributeLabels(){ return[ 'username'=>'Username', 'email' => 'Email', 'password' => 'Password', 'emailSecond' => 'Email (repeat)', 'passwordSecond' => 'Password (repeat)', 'form_of_address' => 'Form of address', 'title' => 'Title', 'name' => 'Name', 'firstName' => 'First name', 'institution' => 'Institution', 'division' => 'Division', 'streetPOBox' => 'Street/P.O. Box', 'postalCode' => 'Postal Code', 'locationCity' => 'Location/City', 'country' => 'County', 'deliveryInstitution' => 'Institution', 'deliveryDivision' => 'Division', 'deliveryStreet' => 'Street/P.O. Box', 'deliveryPostalCode' => 'Postal Code', 'deliveryLocation' => 'Location/City', 'deliveryCountry' => 'County', 'citesCode' => 'Cites Code' ]; } /** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate ()) { $user = new User (); $user->username = $this->username; $user->email = $this->email; $user->form_of_address = $this->form_of_address; $user->title = $this->title; $user->name = $this->name; $user->first_name = $this->firstName; $user->institution = $this->institution; $user->division = $this->division; $user->street_pobox = $this->streetPOBox; $user->postal_code = $this->postalCode; $user->location_city = $this->locationCity; $user->country = $this->country; $user->delivery_institution = $this->deliveryInstitution; $user->delivery_division = $this->deliveryDivision; $user->delivery_street = $this->deliveryStreet; $user->delivery_postal_code = $this->deliveryPostalCode; $user->delivery_location = $this->deliveryLocation; $user->delivery_country = $this->deliveryCountry; $user->cites_code = $this->citesCode; $user->setPassword ( $this->password ); $user->generateAuthKey (); $user->generatePasswordResetToken(); $user->generateActivationKey(); if ($user->save ()) { $auth = Yii::$app->authManager; // // add "validate User" permission // $validateUser = $auth->createPermission('validateUser'); // $validateUser->description = 'Validate a user who wants to sign up'; // $auth->add($validateUser); // //add shopping permission // // $shopping = $auth->createPermission('shopping'); // // $shopping->description = 'Order samples from the database'; // // $auth->add($shopping) //add "alter User" permission // $alterUser = $auth->createPermission('alterUserAdmin'); // $alterUser->description = 'Alter user data a normal user cannot change himself'; // $auth->add($alterUser); // // add "user" role and give this role the "shopping" permission // $loggedInUser = $auth->createRole('user'); // $auth->add($loggedInUser); // //$auth->addChild($loggedInUser, $shopping); // // add 'admin' role and give the admin the permission to validate users // $admin = $auth->createRole('admin'); // $auth->add($admin); // $auth->addChild($admin, $validateUser); //$auth->addChild($admin, $alterUser); //$auth->addChild($admin, $shopping); //first created user is admin by default if(User::find()->count()==0){ $userRole = $auth->getRole('admin'); $auth->assign($userRole, $user->getId()); } $userRole = $auth->getRole('user'); $auth->assign($userRole, $user->getId()); $this->sendValidationEmail(); return $user; } } return null; } /** * Sends a validation link to the admin. * * @return a boolean wether the email was successfully sent. */ public function sendValidationEmail() { /* @var $user User */ $user = User::findOne ( [ 'status' => User::STATUS_INACTIVE, 'email' => $this->email ] ); if ($user) { if(empty($user->__get('activation_key'))) { $user->generateActivationKey(); } return \Yii::$app->mailer->compose ( [ 'html' => 'activationMail-html', 'text' => 'activationMail-text' ], [ 'user' => $user ] )->setFrom ( [ \Yii::$app->params ['supportEmail'] => \Yii::$app->name . ' robot' ] )->setTo ( $this->email )->setSubject ( 'Password reset for ' . \Yii::$app->name )->send (); } else { return false; } } }