Skip to content

Commit

Permalink
Socket: fix rights
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbannov committed Dec 27, 2023
1 parent 1cc656a commit ca41f71
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 121 deletions.
33 changes: 12 additions & 21 deletions common/ASC.Core.Common/Notify/Signalr/SocketServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,37 @@ public class SocketServiceClient
private readonly byte[] _sKey;
private readonly string _url;

public virtual string Hub { get => "default"; }
private static readonly JsonSerializerOptions _options = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

protected virtual string Hub { get => "default"; }

public SocketServiceClient(
ILogger<SocketServiceClient> logger,
IHttpClientFactory clientFactory,
MachinePseudoKeys mashinePseudoKeys,
MachinePseudoKeys machinePseudoKeys,
IConfiguration configuration)
{
_logger = logger;
_clientFactory = clientFactory;
_sKey = mashinePseudoKeys.GetMachineConstant();
_sKey = machinePseudoKeys.GetMachineConstant();
_url = configuration["web:hub:internal"];
_enableSocket = !string.IsNullOrEmpty(_url);
}

public async Task<string> MakeRequest(string method, object data)
public async Task MakeRequest(string method, object data)
{
if (!IsAvailable())
{
return string.Empty;
return;
}
try
{
var request = GenerateRequest(method, data);
var httpClient = _clientFactory.CreateClient();

//async
using var response = await httpClient.SendAsync(request);
await using var stream = await response.Content.ReadAsStreamAsync();
using var streamReader = new StreamReader(stream);
return await streamReader.ReadToEndAsync();
await httpClient.SendAsync(request);
}
catch (Exception e)
{
Expand All @@ -79,15 +79,6 @@ public async Task<string> MakeRequest(string method, object data)
_lastErrorTime = DateTime.Now;
}
}

return null;
}

public async Task<T> MakeRequest<T>(string method, object data)
{
var resultMakeRequest = await MakeRequest(method, data);

return JsonConvert.DeserializeObject<T>(resultMakeRequest);
}

protected void SendNotAwaitableRequest(string method, object data)
Expand All @@ -110,7 +101,7 @@ private HttpRequestMessage GenerateRequest(string method, object data)
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(GetMethod(method));

var jsonData = JsonConvert.SerializeObject(data);
var jsonData = JsonSerializer.Serialize(data, _options);
_logger.DebugMakeRequest(method, jsonData);

request.Content = new StringContent(jsonData, Encoding.UTF8, "application/json");
Expand Down
6 changes: 3 additions & 3 deletions common/ASC.Core.Common/Quota/QuotaSocketManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public class QuotaSocketManager : SocketServiceClient
{
private readonly TenantManager _tenantManager;

public override string Hub => "files";
protected override string Hub => "files";

public QuotaSocketManager(
ILogger<SocketServiceClient> logger,
IHttpClientFactory clientFactory,
MachinePseudoKeys mashinePseudoKeys,
MachinePseudoKeys machinePseudoKeys,
TenantManager tenantManager,
IConfiguration configuration) : base(logger, clientFactory, mashinePseudoKeys, configuration)
IConfiguration configuration) : base(logger, clientFactory, machinePseudoKeys, configuration)
{
_tenantManager = tenantManager;
}
Expand Down
4 changes: 2 additions & 2 deletions common/ASC.Socket.IO/app/controllers/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
res.end();
});

router.post("/markasnew-file", (req, res) => {
router.post("/mark-as-new-file", (req, res) => {
files.markAsNewFiles(req.body);
res.end();
});

router.post("/markasnew-folder", (req, res) => {
router.post("/mark-as-new-folder", (req, res) => {
files.markAsNewFolders(req.body);
res.end();
});
Expand Down
83 changes: 62 additions & 21 deletions common/ASC.Socket.IO/app/hubs/files.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = (io) => {
const logger = require("../log");
module.exports = (io) => {
const logger = require("../log.js");
const moment = require("moment");
const filesIO = io; //TODO: Restore .of("/files");
Expand Down Expand Up @@ -141,42 +142,82 @@
filesIO.to(room).emit("s:modify-folder", { cmd, id, type, data });
}

function createFile({ fileId, room, data } = {}) {
logger.info(`create new file ${fileId} in room ${room}`);
modifyFolder(room, "create", fileId, "file", data);
function createFile({ id, room, data, userIds } = {}) {
logger.info(`create new file ${id} in room ${room}`);

if(userIds)
{
userIds.forEach(userId => modifyFolder(`${room}-${userId}`, "create", id, "file", data));
}
else
{
modifyFolder(room, "create", id, "file", data);
}
}

function createFolder({ folderId, room, data, userIds } = {}) {
logger.info(`create new folder ${folderId} in room ${room}`);
logger.info(`create new folder length ${userIds.length}`);
function createFolder({ id, room, data, userIds } = {}) {
logger.info(`create new folder ${id} in room ${room}`);
if(userIds)
{
userIds.forEach(userId => modifyFolder(`${room}-${userId}`, "create", folderId, "folder", data));
userIds.forEach(userId => modifyFolder(`${room}-${userId}`, "create", id, "folder", data));
}
else
{
modifyFolder(room, "create", folderId, "folder", data);
modifyFolder(room, "create", id, "folder", data);
}
}

function updateFile({ fileId, room, data } = {}) {
logger.info(`update file ${fileId} in room ${room}`);
modifyFolder(room, "update", fileId, "file", data);
function updateFile({ id, room, data, userIds } = {}) {
logger.info(`update file ${id} in room ${room}`);

if(userIds)
{
userIds.forEach(userId => modifyFolder(`${room}-${userId}`, "update", id, "file", data));
}
else
{
modifyFolder(room, "update", id, "file", data);
}
}

function updateFolder({ folderId, room, data } = {}) {
logger.info(`update folder ${folderId} in room ${room}`);
modifyFolder(room, "update", folderId, "folder", data);
function updateFolder({ id, room, data, userIds } = {}) {
logger.info(`update folder ${id} in room ${room}`);
modifyFolder(room, "update", id, "folder", data);

if(userIds)
{
userIds.forEach(userId => modifyFolder(`${room}-${userId}`, "update", id, "folder", data));
}
else
{
modifyFolder(room, "update", id, "folder", data);
}
}

function deleteFile({ fileId, room } = {}) {
logger.info(`delete file ${fileId} in room ${room}`);
modifyFolder(room, "delete", fileId, "file");
function deleteFile({ id, room, userIds } = {}) {
logger.info(`delete file ${id} in room ${room}`);

if(userIds)
{
userIds.forEach(userId => modifyFolder(`${room}-${userId}`, "delete", id, "file"));
}
else
{
modifyFolder(room, "delete", id, "file");
}
}

function deleteFolder({ folderId, room } = {}) {
logger.info(`delete file ${folderId} in room ${room}`);
modifyFolder(room, "delete", folderId, "folder");
function deleteFolder({ id, room, userIds } = {}) {
logger.info(`delete folder ${id} in room ${room}`);

if(userIds)
{
userIds.forEach(userId => modifyFolder(`${room}-${userId}`, "delete", id, "folder"));
}
else
{
modifyFolder(room, "delete", id, "folder");
}
}

function markAsNewFile({ fileId, count, room } = {}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ private async Task DoAsync<TTo>(IServiceScope scope, TTo tto)
var folderDao = scope.ServiceProvider.GetService<IFolderDao<TTo>>();
var fileSecurity = scope.ServiceProvider.GetService<FileSecurity>();
var socketManager = scope.ServiceProvider.GetService<SocketManager>();
var userManager = scope.ServiceProvider.GetService<UserManager>();

//TODO: check on each iteration?
var toFolder = await folderDao.GetFolderAsync(tto);
Expand Down Expand Up @@ -212,18 +211,13 @@ private async Task DoAsync<TTo>(IServiceScope scope, TTo tto)

needToMark.AddRange(moveOrCopyFilesTask);

var admins = await userManager.GetUsers(true, EmployeeStatus.Active, null, null, null, null, null, null, null, true, 0, 0).Select(r=> r.Id).ToListAsync();
admins.Add(CurrentTenant.OwnerId);

foreach (var folder in moveOrCopyFoldersTask)
{
if (toFolder.FolderType != FolderType.Archive && !DocSpaceHelper.IsRoom(folder.FolderType))
{
needToMark.AddRange(await GetFilesAsync(scope, folder));
}

var whoCanRead = await fileSecurity.WhoCanReadAsync(folder);
await socketManager.CreateFolderAsync(folder, admins.Concat(whoCanRead).Distinct().ToList());
await socketManager.CreateFolderAsync(folder);
}

var ntm = needToMark.Distinct();
Expand Down
Loading

0 comments on commit ca41f71

Please sign in to comment.