public function toArray() { $result = ConvertUtil::objectToArray($this, $dst, ['id' => 'uid', 'username' => 'account', 'nick', 'motto', 'token']); $result['avatar'] = $this->avatar ? PIC_HOST . $this->avatar : null; $result['present'] = JegarnUtil::getUserPresent($this->id); return $result; }
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; }
public function updateRoster(Roster $model) { if (!$model->uid || !$model->target_id) { return new ApiResponse(Code::FAIL_ROSTER_NOT_EXISTS, 'uid or target_id is empty'); } if ($model->status != Roster::STATUS_UNSUBSCRIBE && $model->status != Roster::STATUS_AGREE && $model->status != Roster::STATUS_REFUSED && $model->status != Roster::STATUS_BLACK) { return new ApiResponse(Code::FAIL_ROSTER_STATUS, 'status must be unsubscribe or receive or refused or blacklist'); } if ($resp = $this->checkRoster($model)) { return $resp; } $modelCopy = clone $model; if ($resp = $this->getRoster($modelCopy)) { return $resp; } $model->id = $modelCopy->id; if ($model->uid != $modelCopy->uid) { return new ApiResponse(Code::FAIL_ROSTER_NOT_EXISTS, 'other\'s roster'); } $fullSame = true; $compareField = ['status', 'remark', 'group_id', 'rank']; foreach ($compareField as $f) { if ($model->{$f} != $modelCopy->{$f}) { $fullSame = false; break; } } if ($fullSame) { return null; } $model->update_time = time(); $dbManager = Db::getInstance(); // receive when i am her friend or her ask to make $roster = null; if ($model->status == Roster::STATUS_AGREE) { $roster = new Roster(); $roster->uid = $model->target_id; $roster->target_id = $model->uid; if ($resp = $this->getRoster($roster)) { return $resp; } if ($roster->status != Roster::STATUS_REQUEST && $roster->status != Roster::STATUS_AGREE) { return new ApiResponse(Code::FAIL_ROSTER_STATUS, 'status must be ask or agree'); } } $dbManager->beginTransaction(); $statement = $dbManager->prepare(self::UPDATE_ROSTER); if (!$statement->execute([$model->status, $model->update_time, $model->remark, $model->group_id, $model->rank, $model->id])) { $dbManager->rollBack(); return new ApiResponse(Code::FAIL_DATABASE_ERROR, 'update roster failed'); } if ($roster && $roster->status == Roster::STATUS_REQUEST) { $roster->status = Roster::STATUS_AGREE; $roster->update_time = time(); if (!$statement->execute([$roster->status, $roster->update_time, $roster->remark, $roster->group_id, $roster->rank, $roster->id])) { $dbManager->rollBack(); return new ApiResponse(Code::FAIL_DATABASE_ERROR, 'update target roster failed'); } JegarnUtil::sendFriendAgreeNotification($roster->target_id, $roster->uid); } $dbManager->commit(); // if status not changed, it may change friend attributes. so don not need notify if ($modelCopy->status != Roster::STATUS_AGREE && $model->status == Roster::STATUS_AGREE) { JegarnUtil::sendFriendAgreeNotification($model->target_id, $model->uid); } else { if ($modelCopy->status != Roster::STATUS_REFUSED && $model->status == Roster::STATUS_REFUSED) { JegarnUtil::sendFriendRefusedNotification($model->target_id, $model->uid); } } return null; }
public function removeGroupCompleted(Group $model) { $dbManager = Db::getInstance(); $dbManager->beginTransaction(); $statement = $dbManager->prepare(self::DELETE_GROUP); if (!$statement->execute([$model->id])) { $dbManager->rollBack(); return new ApiResponse(Code::FAIL_DATABASE_ERROR, 'delete group failed'); } if ($resp = GroupUserManager::getInstance()->removeAllGroupUser($model)) { $dbManager->rollBack(); return $resp; } $dbManager->commit(); JegarnUtil::removeGroup($model->id); return null; }
public function removeUser(User $model) { $dbManager = Db::getInstance(); if ($model->id) { $statement = $dbManager->prepare(self::REMOVE_USER_BY_UID); $statement->bindValue(1, $model->id, PDO::PARAM_INT); // this add to get user's user name if (!$model->username) { if ($resp = $this->getUser($model)) { return $resp; } } } else { if ($model->username) { $statement = $dbManager->prepare(self::REMOVE_USER_BY_USERNAME); $statement->bindValue(1, $model->username, PDO::PARAM_STR); } else { return new ApiResponse(Code::FAIL_USER_UID_OR_NAME_NOT_EXISTS, null); } } if (!$statement->execute()) { return new ApiResponse(Code::FAIL_DATABASE_ERROR, null); } JegarnUtil::removeUser($model->username); return null; }