Exemplo n.º 1
0
 /**
  * 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');
     }
 }
Exemplo n.º 2
0
 public function actionUpdate($id)
 {
     $id = new \MongoId($id);
     $user = User::findOne(['_id' => $id]);
     $user->load($this->getParams(), '');
     if ($user->save() === false && !$user->hasErrors()) {
         throw new ServerErrorHttpException('Failed to update the object for unknown reason.');
     }
     //update the language information in token
     Token::updateAll(['language' => $user->language]);
     return $user;
 }
Exemplo n.º 3
0
 /**
  * Login
  *
  * <b>Request Type</b>: POST<br/><br/>
  * <b>Request Endpoint</b>:http://{server-domain}/chat/site/login<br/><br/>
  * <b>Content-type</b>: application/json<br/><br/>
  * <b>Summary</b>: This api is used for the help desk to login.
  * <br/><br/>
  *
  * <b>Request Params</b>:<br/>
  *     email: string, the user email, required<br/>
  *     password: string, the user password, required<br/>
  *     <br/><br/>
  *
  * <b>Response Params:</b><br/>
  *     ack: integer, mark the create result, 0 means create successfully, 1 means create fail<br/>
  *     msg: string, if create fail, it contains the error message<br/>
  *     data: array, json array to describe the users detail information<br/>
  *     <br/><br/>
  *
  * <b>Request Example:</b><br/>
  * <pre>
  * {
  *     "email"    : "*****@*****.**",
  *     "password" : "abc123"
  * }
  * </pre>
  * <br/><br/>
  *
  * <b>Response Example</b>:<br/>
  * <pre>
  * {
  *    'ack'  : 1,
  *    'data' : {
  *        "accessToken" : "7f2d1e92-9629-8429-00be-2d9c6d64acdb",
  *        "userInfo"    : {
  *            "name"   : "harry",
  *            "avatar" : "path/to/avatar"
  *        }
  *    }
  * }
  * </pre>
  */
 public function actionLogin()
 {
     $params = $this->getParams();
     $deviceToken = $this->getParams('deviceToken');
     $environment = $this->getParams('environment');
     if (empty($params['email']) || empty($params['password'])) {
         throw new BadRequestHttpException("parameters missing");
     }
     $helpdesk = HelpDesk::getByEmail($params['email']);
     if (empty($helpdesk)) {
         throw new ForbiddenHttpException("用戶不存在");
     }
     if (!$helpdesk->isActivated) {
         throw new ForbiddenHttpException("用戶未激活,请激活后使用");
     }
     if (!$helpdesk->isEnabled) {
         throw new ForbiddenHttpException("该账号已被禁用,请与管理员联系");
     }
     if ($helpdesk->validatePassword($params['password'])) {
         $tokens = Token::getUnexpiredByUserId($helpdesk->_id);
         if (!empty($tokens)) {
             $data = ['isForcedOffline' => true, 'id' => $helpdesk->_id . ''];
             $accountId = $tokens[0]->accountId;
             Yii::$app->tuisongbao->triggerEvent(ChatConversation::EVENT_FORCED_OFFLINE, $data, [ChatConversation::CHANNEL_GLOBAL . $accountId]);
             //deviceToken changed, push forcedOffline
             if (empty($deviceToken) && !empty($helpdesk->deviceToken) || !empty($deviceToken) && !empty($helpdesk->deviceToken) && $deviceToken != $helpdesk->deviceToken) {
                 $extra = ['deskId' => $helpdesk->_id . '', 'sentTime' => TimeUtil::msTime()];
                 ChatConversation::pushMessage($helpdesk->_id, ChatConversation::EVENT_FORCED_OFFLINE, $extra);
             }
             Token::updateAll(['$set' => ['expireTime' => new \MongoDate()]], ['_id' => ['$in' => Token::getIdList($tokens)]]);
         }
         $isFirstLogin = empty($helpdesk->lastLoginAt);
         $accessToken = Token::createByHelpDesk($helpdesk);
         if (isset($deviceToken)) {
             $helpdesk->loginDevice = HelpDesk::MOBILEAPP;
         } else {
             $helpdesk->loginDevice = HelpDesk::BROWSER;
         }
         $helpdesk->deviceToken = $deviceToken;
         $helpdesk->environment = $environment;
         $helpdesk->lastLoginAt = new \MongoDate();
         $helpdesk->save(true, ['deviceToken', 'loginDevice', 'environment', 'lastLoginAt']);
         $userInfo = ['badge' => $helpdesk->badge, 'name' => $helpdesk->name, 'email' => $helpdesk->email, 'language' => $helpdesk->language, 'avatar' => empty($helpdesk->avatar) ? '' : $helpdesk->avatar, 'id' => (string) $helpdesk->_id, 'accountId' => (string) $helpdesk['accountId'], 'notificationType' => $helpdesk->notificationType, 'isFirstLogin' => $isFirstLogin];
         return ["accessToken" => $accessToken['accessToken'], 'userInfo' => $userInfo];
     } else {
         throw new ForbiddenHttpException("密码错误");
     }
 }
Exemplo n.º 4
0
 /**
  * Update language
  * @param string $lauguage
  * @param string $token
  * @return
  */
 public static function channgeLanguage($token, $lauguage)
 {
     return Token::updateAll(['language' => $lauguage], ['accessToken' => $token]);
 }