示例#1
0
 public function addGroup(Group $model)
 {
     if ($resp = $this->checkGroup($model)) {
         return $resp;
     }
     if (!$model->checkType()) {
         return new ApiResponse(Code::FAIL_GROUP_TYPE, null);
     }
     $type = $model->type == Group::TYPE_GROUP ? 'g' : 'r';
     $model->icon = 'group/default/' . $type . rand(0, 9) . '.jpg';
     $model->create_time = time();
     $dbManager = Db::getInstance();
     $dbManager->beginTransaction();
     $statement = $dbManager->prepare(self::ADD_GROUP);
     if (!$statement->execute([$model->uid, $model->type, $model->name, $model->create_time, $model->description, $model->icon])) {
         $dbManager->rollBack();
         return new ApiResponse(Code::FAIL_DATABASE_ERROR, 'create group failed');
     }
     $model->id = $dbManager->lastInsertId();
     $groupUser = new GroupUser();
     $groupUser->uid = $model->uid;
     $groupUser->status = GroupUser::STATUS_AGREE;
     $groupUser->permission = GroupUser::PERMISSION_ROOT;
     if ($resp = GroupUserManager::getInstance()->addGroupUser($model, $groupUser)) {
         $dbManager->rollBack();
         return $resp;
     }
     $dbManager->commit();
     if ($model->type == Group::TYPE_GROUP) {
         JegarnUtil::joinGroup($model->id, $model->uid);
     } else {
         JegarnUtil::joinChatroom($model->id, $model->uid);
     }
     return null;
 }
示例#2
0
 public function addUser(User $model)
 {
     if (empty($model->username)) {
         return new ApiResponse(Code::FAIL_EMPTY_ACCOUNT, '');
     }
     if ($resp = $this->checkPassword($model->password)) {
         return $resp;
     }
     $gender = rand(0, 10) > 5 ? 'g' : 'b';
     $model->avatar = 'avatar/default/' . $gender . rand(0, 9) . '.jpg';
     $model->token = TextUtil::generateGUID();
     $model->create_time = time();
     $model->reg_ip = ApiRequest::getRemoteIp();
     $password = $this->enCryptPassword($model->password);
     $dbManager = Db::getInstance();
     $statement = $dbManager->prepare(self::ADD_USER);
     if (!$statement->execute([$model->username, $password, $model->create_time, $model->nick, $model->motto, $model->avatar, $model->token, $model->reg_ip])) {
         return new ApiResponse(Code::FAIL_USER_NAME_ALREADY_EXISTS, null);
     }
     $model->id = $dbManager->lastInsertId();
     JegarnUtil::addUser($model->id, $model->username, $model->token);
     JegarnUtil::sendUserSystemTextChatMessage($model->id, 'welcome to jegarn');
     // add login record
     $loginLog = new LoginLog($model->id, LoginLog::STATUS_SUCCESS);
     LoginLogManager::getInstance()->addLog($loginLog);
     // make friends with counter, join group 'Counter Group', join chatroom 'Counter Room'
     $targetId = self::COUNTER_UID;
     $groupId = self::COUNTER_GROUP_ID;
     $chatroomId = self::COUNTER_CHATROOM_ID;
     $sqlList = ["INSERT INTO `m_roster`(uid,target_id,status,create_time,update_time,remark,group_id,rank) VALUES ({$targetId},{$model->id},3,{$model->create_time},0,NULL,0,0)", "INSERT INTO `m_roster`(uid,target_id,status,create_time,update_time,remark,group_id,rank) VALUES ({$model->id},{$targetId},3,{$model->create_time},0,NULL,0,0)", "INSERT into `m_group_user`(gid,uid,permission,create_time,status,remark) values({$groupId},{$model->id},0,{$model->create_time},3,NULL)", "INSERT into `m_group_user`(gid,uid,permission,create_time,status,remark) values({$chatroomId},{$model->id},0,{$model->create_time},3,NULL)"];
     foreach ($sqlList as $sql) {
         $dbManager->exec($sql);
     }
     JegarnUtil::joinGroup($groupId, $model->id);
     JegarnUtil::joinChatroom($chatroomId, $model->id);
     return null;
 }
示例#3
0
 public function manageUser(GroupUser $model, User $manageUser)
 {
     $checkStatus = $model->checkStatus();
     $checkPermission = $model->checkPermission();
     if (!$checkStatus && !$checkPermission) {
         return null;
     }
     if (!($dbModel = $this->getGroupUserByGidUid($model->gid, $model->uid))) {
         return new ApiResponse($this->getLastErrorCode(), $this->getLastErrorString());
     }
     $model->id = $dbModel->id;
     // nothing changed as up
     if ($checkStatus && $model->status == $dbModel->status && $checkPermission && $model->permission == $dbModel->permission) {
         return null;
     }
     $groupManager = GroupManager::getInstance();
     if (!($group = $groupManager->getGroupById($dbModel->gid))) {
         return new ApiResponse($groupManager->getLastErrorCode(), null);
     }
     if ($group->type == Group::TYPE_CHATROOM) {
         return new ApiResponse(Code::FAIL_PERMISSION_DENY, 'chatroom can not manage');
     }
     if (!($userGroupUser = $this->getGroupUserByGidUid($dbModel->gid, $manageUser->id))) {
         return new ApiResponse($this->getLastErrorCode(), $this->getLastErrorString());
     }
     if ($userGroupUser->permission != GroupUser::PERMISSION_ADMIN && $userGroupUser->permission != GroupUser::PERMISSION_ROOT) {
         return new ApiResponse(Code::FAIL_PERMISSION_DENY, 'normal user');
     }
     if ($userGroupUser->permission == GroupUser::PERMISSION_ADMIN && ($dbModel->permission == GroupUser::PERMISSION_ADMIN || $dbModel->permission == GroupUser::PERMISSION_ROOT)) {
         return new ApiResponse(Code::FAIL_PERMISSION_DENY, 'admin only can manage normal people');
     }
     if ($userGroupUser->permission == GroupUser::PERMISSION_ADMIN && $checkPermission) {
         return new ApiResponse(Code::FAIL_PERMISSION_DENY, 'admin only can not manage permission');
     }
     if ($userGroupUser->permission == GroupUser::PERMISSION_ROOT && $checkPermission && $model->permission == GroupUser::PERMISSION_ROOT) {
         return new ApiResponse(Code::FAIL_PERMISSION_DENY, 'always one root');
     }
     if ($checkStatus && $checkPermission) {
         if ($resp = $this->updateGroupUserStatusAndPermission($model)) {
             return $resp;
         }
     } else {
         if ($checkStatus) {
             if ($resp = $this->updateGroupUserStatus($model)) {
                 return $resp;
             }
         } else {
             if ($resp = $this->updateGroupUserPermission($model)) {
                 return $resp;
             }
         }
     }
     $model->gid = $dbModel->gid;
     $model->create_time = $dbModel->create_time;
     $model->uid = $dbModel->uid;
     $model->remark = $dbModel->remark;
     if ($dbModel->status != GroupUser::STATUS_AGREE && $model->status == GroupUser::STATUS_AGREE) {
         JegarnUtil::joinGroup($model->gid, $model->uid);
         JegarnUtil::sendGroupAgreeNotification($group->uid, $model->uid, $group->id, $group->name);
     } else {
         if ($dbModel->status != GroupUser::STATUS_REFUSED && $model->status == GroupUser::STATUS_REFUSED) {
             JegarnUtil::sendGroupRefusedNotification($group->uid, $model->uid, $group->id, $group->name);
         }
     }
     return null;
 }