public function deleteTag($accountId, $name) { Account::updateAll(['$pull' => ['tags' => ['name' => $name]]], ['_id' => $accountId]); Member::updateAll(['$pull' => ['tags' => $name]], ['accountId' => $accountId, 'tags' => $name]); Campaign::updateAll(['$pull' => ['tags' => $name]], ['accountId' => $accountId, 'tags' => $name]); $data = ['type' => 'tag_deleted', 'account_id' => $accountId, 'name' => $name]; $this->notifyModules($data); }
/** * Create tags * @param array $tags * @return boolean */ public function create($tags) { if (empty($tags) || !is_array($tags)) { return false; } $addTags = []; foreach ($tags as $tagName) { $addTags[] = ['name' => $tagName]; } return (bool) Account::updateAll(['$addToSet' => ['tags' => ['$each' => $addTags]]], ['_id' => $this->accountId]); }
/** * Activate a module * * <b>Request Type </b>:PUT * <b>Request Endpoints </b>: http://{server-domain}/api/management/module/activate-module * <b>Content-type</b>: application/json<br/><br/> * <b>Summary</b>: This api is used for the user to activate a extension module. * * <b>Request Example </b>: * <pre> * http://{server-domain}/api/management/module/activate-module * </pre> * <pre> * { * "name" : "customer", * } * </pre> * **/ public function actionActivateModule() { $moduleName = $this->getParams('name'); $accountId = $this->getAccountId(); $moduleNames = Yii::$app->extModule->getDependencyModules($moduleName); $account = Account::findByPk($accountId); if (in_array($moduleName, $account->enabledMods)) { throw new BadRequestHttpException(\Yii::t('common', 'function_has_been_activated')); } $updateAccountResult = Account::updateAll(['$addToSet' => ['enabledMods' => ['$each' => $moduleNames]]], ['_id' => $accountId]); if ($updateAccountResult) { $updateTokenResult = Token::updateAll(['$addToSet' => ['enabledMods' => ['$each' => $moduleNames]]], ['accountId' => $accountId]); if ($updateTokenResult) { $installFilePath = Yii::getAlias('@backend') . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'Install.php'; if (file_exists($installFilePath)) { require_once $installFilePath; $className = 'backend\\modules\\' . $moduleName . '\\Install'; if (class_exists($className)) { $installObj = Yii::createObject($className); $installObj->run($accountId); } } list($menus, $mods) = Yii::$app->extModule->getMenuAndExt($moduleName); $dbMenus = $account->menus; $dbMods = $account->mods; foreach ($menus as $moduleName => $menu) { if (!isset($dbMenus[$moduleName])) { $dbMenus[$moduleName] = []; } $dbMenus[$moduleName] = ArrayHelper::merge($dbMenus[$moduleName], $menu); } $account->menus = $dbMenus; foreach ($mods as $mod) { $isInDB = false; foreach ($dbMods as $dbMod) { if (!empty($dbMod['name']) && !empty($mod['name']) && $dbMod['name'] == $mod['name']) { $isInDB = true; break; } } if (!$isInDB) { $dbMods[] = $mod; } } $account->mods = $dbMods; $account->save(true, ['menus', 'mods']); } else { throw new ServerErrorHttpException('Activate fail'); } } else { throw new ServerErrorHttpException('Activate fail'); } }
/** * Validate code when activate user * @param $code, String. * @return String, error code or userId * * @author Sara Zhang */ public static function validateCode($code, $isDeleted = true) { if (empty($code)) { return self::LINK_INVALID; } $validation = Validation::findOne(['code' => $code]); if (empty($validation)) { return self::LINK_INVALID; } if (empty($validation->expire) || MongodbUtil::isExpired($validation->expire)) { return self::LINK_EXPIRED; } $userId = $validation->userId; if ($validation->toValidateAccount) { $user = User::findOne(['_id' => $userId]); $attributes = ['status' => Account::STATUS_ACTIVATED, 'trialStartAt' => new \MongoDate(), 'trialEndAt' => new \MongoDate(strtotime("+30 day"))]; Account::updateAll($attributes, ['_id' => $user->accountId]); } if ($isDeleted) { $validation->delete(); } return $userId; }
/** * Create tag * * <b>Request Type: </b> POST<br/> * <b>Request Endpoint</b>:http://{server-domain}/api/channel/tags<br/><br/> * <b>Response Content-type</b>: application/json<br/><br/> * <b>Summary </b>: This api is used for create tag. * * <b>Request Parameters: </b><br/> * name: the name of the tag * * <b>Response Params:</b><br/> * msg: string, if create fail, it contains the error message<br/> * <br/><br/> * * <br/><br/> * * <b>Response Example</b>:<br/> * <pre> * { * "msg": "OK" * } * </pre> */ public function actionCreate() { $result = ['msg' => 'OK']; $tags = $this->getParams('tags'); $isAutoScanFollower = $this->getParams('isAutoScanFollower'); $accountId = $this->getAccountId(); $account = Account::findOne(['_id' => new \MongoId($accountId)]); if (empty($account)) { throw new GoneHttpException("no such account"); } if (!empty($isAutoScanFollower) && $isAutoScanFollower == true) { if (empty($tags) || !is_array($tags)) { throw new BadRequestHttpException("Error Processing Request", 1); } } $tag = []; foreach ($tags as $item) { $tag[] = ['name' => $item]; } if (!Account::updateAll(['$addToSet' => ['tags' => ['$each' => $tag]]], ['_id' => $accountId])) { throw new ServerErrorHttpException("update tags failed"); } return $result; }
/** * create a user by email(generate-by-email) */ public function actionGenerateByEmail($email) { $email = mb_strtolower($email); $user = User::getByEmail($email); if (!empty($user)) { echo 'email is used' . PHP_EOL; return; } $name = Yii::$app->params['defaultName']; $accountId = Account::create('', '', $name); $attributes = ['status' => Account::STATUS_ACTIVATED, 'availableExtMods' => Yii::$app->params['extMods'], 'serviceStartAt' => new \MongoDate()]; Account::updateAll($attributes, ['_id' => $accountId]); $salt = StringUtil::rndString(6); $password = User::encryptPassword(md5(Yii::$app->params['defaultPwd']), $salt); $user = new User(); $user->email = $email; $user->accountId = $accountId; $user->name = $name; $user->role = User::ROLE_ADMIN; $user->isActivated = User::ACTIVATED; $user->avatar = Yii::$app->params['defaultAvatar']; $user->language = Yii::$app->params['defaultLanguage']; $user->salt = $salt; $user->password = $password; if (!$user->save()) { Account::deleteAll(['_id' => $accountId]); SensitiveOperation::deleteAll(['accountId' => $accountId]); MessageTemplate::deleteAll(['accountId' => $accountId]); echo 'create account fail' . PHP_EOL; } else { echo 'create account successfully' . PHP_EOL; } }