/** * Register billing account * * <b>Request Type</b>: POST<br/><br/> * <b>Request Endpoint</b>:http://{server-domain}/site/register<br/><br/> * <b>Content-type</b>: application/json<br/><br/> * <b>Summary</b>: This api is used for registering user. * <br/><br/> * * <b>Request Params</b>:<br/> * name: string, the user name<br/> * email: string, the user email<br/> * password: string, the user password<br/> * <br/><br/> * * <b>Response Params:</b><br/> * ack: integer, mark the create result, 1 means create successfully, 0 means create fail<br/> * message: string, if create fail, it contains the error message<br/> * data: array, json array to describe all users detail information<br/> * <br/><br/> * * <b>Request Example:</b><br/> * <pre> * { * "name" : "harrysun", * "email" : "*****@*****.**", * "password" : "abc123_" * } * </pre> * <br/><br/> * * <b>Response Example</b>:<br/> * <pre> * { * 'ack' : 1, * 'message': '' * } * </pre> */ public function actionRegister() { $data = $this->getParams(); $account = new Account(); $account->save(); $user = new User(); $user->name = $data['name']; $user->email = $data['email']; $user->salt = StringUtil::rndString(6); $user->password = User::encryptPassword($data['password'], $user->salt); $user->accountId = $account->_id; $user->role = User::ROLE_ADMIN; $user->isActivated = User::NOT_ACTIVATED; $user->avatar = Yii::$app->params['defaultAvatar']; $user->language = 'zh_cn'; if ($user->validate()) { // all inputs are valid if ($user->save()) { $validation = new Validation(); $validation->userId = $user->_id; $validation->code = StringUtil::uuid(); $validation->expire = new \MongoDate(strtotime('+1 day')); if ($validation->save()) { $mail = Yii::$app->mail; $host = Yii::$app->request->hostInfo; $vars = ['name' => $user->name, 'link' => $host . '/api/old-site/activate?code=' . $validation->code, 'host' => $host]; $mail->setView('//mail/register', $vars, '//layouts/email'); $mail->sendMail($user->email, '欢迎注册WeMarketing'); return ["ack" => 1, "message" => 'Register success.']; } else { return ["ack" => 0, "message" => 'Validation save fail.']; } } else { return ["ack" => 0, "message" => 'Register user fail.']; } } else { // validation failed: $errors is an array containing error messages $errors = $user->errors; //revert the accout data Account::deleteAll(['_id' => $account->_id]); return ["ack" => 0, "message" => $errors]; } }
/** * Create a new account and initialize it * @param string $company * @param string $mobile * @param string $priceType * @throws ServerErrorHttpException * @return MongoId */ public static function create($company, $phone, $name, $priceType = self::PRICE_TYPE_FREE) { $account = new Account(); $account->priceType = $priceType; $account->company = $company; $account->phone = $phone; $account->name = $name; $account->availableExtMods = ['member', 'product', 'microsite', 'helpdesk']; // add 4 basic mods into extensions #3191 $account->generateKey(); $enabledMods = Yii::$app->params['coreMods']; $result = Yii::$app->extModule->getMergedConfig($enabledMods); $account->menus = $result['menus']; $account->mods = $result['mods']; if ($account->save()) { self::afterCreateAccount($account); return $account->_id; } throw new ServerErrorHttpException("Save account failed"); }