Overrides & Members

This commit is contained in:
JacobTech 2024-03-31 23:57:12 -04:00
parent e94151e814
commit 82ccdd8307
9 changed files with 159 additions and 29 deletions

View File

@ -107,7 +107,7 @@ public class API
{
IEnumerable<PublicServer> isl = InternalServers.Where(a => (a.Domain == Domain && a.ApiVersion == Version));
if (isl.Any()) return isl.First();
s = await PublicServer.GetServer(Domain, Version, Secure);
s = await PublicServer.GetServer(Domain, Version, Secure, GenerateEncryption, LogConsole);
}
catch (Exception e)
{

View File

@ -13,11 +13,11 @@
<RepositoryUrl>https://github.com/JacobTech-com/Luski.net</RepositoryUrl>
<IncludeSymbols>True</IncludeSymbols>
<FileVersion>1.0.0</FileVersion>
<Version>2.0.0-alpha65</Version>
<Version>2.0.0-alpha76</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Luski.Shared" Version="1.1.0-alpha19" />
<PackageReference Include="Luski.Shared" Version="1.1.0-alpha21" />
<PackageReference Include="websocketsharp.core" Version="1.0.0" />
</ItemGroup>

View File

@ -203,6 +203,17 @@ public partial class PublicServer
FailedSystems.Add(new("Auto Status", "Failed to set status on the server", e));
}
#endregion
#region GetRoles
try
{
_ = await GetRoles();
}
catch (Exception e)
{
FailedSystems.Add(new("Get Roles", "Failed to get roles on the server", e));
}
#endregion
#region Local Storage Cleanup
try

View File

@ -29,6 +29,7 @@ public partial class PublicServer : Server
public List<SocketChannel> chans { get; } = new();
public List<SocketCategory> cats { get; } = new();
public List<Role> roles { get; } = new();
public List<RoleOverride> roleso { get; } = new();
public SocketAppUser User { get; private set; } = null!;
@ -210,6 +211,47 @@ public partial class PublicServer : Server
roles.Add(role);
return role;
}
public async Task<RoleOverride> GetRoleOverride(long id)
{
RoleOverride[] r = roleso.Where(s => s.ID == id).ToArray();
if (r.Length > 0) return r[0];
UserRoleOverrideSTC s = await GetFromServer("SocketOverrides/RoleOverride/" + id.ToString(), UserRoleOverrideSTCContext.Default.UserRoleOverrideSTC, CancellationToken.None);
RoleOverride role = new()
{
ID = s.Id,
BadPermissions = s.BadPermissions,
GoodPermissions = s.GoodPermissions,
ParentRoleID = s.RoleID,
Server = this
};
roleso.Add(role);
return role;
}
public async Task<Role[]> GetRoles()
{
RolesSTC s = await GetFromServer("SocketRole/GetAll", RolesSTCContext.Default.RolesSTC, CancellationToken.None);
roles.Clear();
foreach (var ServerRole in s.Roles)
{
roles.Add(new Role()
{
Server = this,
ID = ServerRole.ID,
Color = new(ServerRole.Color),
Description = ServerRole.Description,
DisplayName = ServerRole.DisplayName,
MembersListID = ServerRole.Members,
Name = ServerRole.Name,
Index = ServerRole.Index,
ServerPermissions = ServerRole.ServerPermissions
});
}
return roles.ToArray();
}
public async Task<SocketMessage> SendMessage<TChannel>(TChannel channel, string msg, SocketMessage? ReplyTo = null,
SocketChannelProfile? FakeProfile = null) where TChannel : SocketChannel, new()
@ -344,6 +386,7 @@ public partial class PublicServer : Server
Server = this,
Color = new(request.Color)
};
chans.Add(bob);
return bob;
}
@ -364,7 +407,7 @@ public partial class PublicServer : Server
new ChannelPostCTS()
{
Name = Convert.ToBase64String(Encoding.UTF8.GetBytes(Name)),
Description = Convert.ToBase64String(Encoding.UTF8.GetBytes(Description)),
Description = Convert.ToBase64String(Encoding.UTF8.GetBytes(Decription)),
EncoderTypes = new[] { EncoderType.UTF16 },
EncryptionKeys = new long[] { 0 },
DescriptionEncoderType = EncoderType.UTF8,
@ -385,7 +428,7 @@ public partial class PublicServer : Server
{
ID = res.ID,
CategoryID = res.Parent,
Description = Description,
Description = Decription,
DescriptionEncoderType = res.DescriptionEncoderType,
DescriptionEncryptionKey = res.DescriptionEncryptionKey,
EncoderTypes = res.EncoderTypes,
@ -439,7 +482,7 @@ public partial class PublicServer : Server
}
Tuser u = new();
if (u is SocketUser)
if (typeof(Tuser).FullName == typeof(SocketUser).FullName)
{
u = new()
{

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Luski.Shared.PublicServers.V1.Enums;
@ -17,13 +18,17 @@ public class Role
public required long[] MembersListID { get; init; } = default!;
private List<SocketUser>? RawUsers = null;
public Task<SocketUser[]> GetMembers()
public async Task<SocketUser[]> GetMembers()
{
if (RawUsers is null)
{
RawUsers = new();
foreach (long m in MembersListID)
{
RawUsers.Add(await Server.GetUser<SocketUser>(m, CancellationToken.None));
}
}
return Task.FromResult(RawUsers!.ToArray());
return RawUsers!.ToArray();
}
}

View File

@ -4,7 +4,7 @@ using Luski.Shared.PublicServers.V1.Shared;
namespace Luski.net.Structures.Public;
public class RoleOveride
public class RoleOverride
{
public long ID { get; init; }
public long ParentRoleID { get; init; }
@ -12,15 +12,11 @@ public class RoleOveride
public ServerPermission GoodPermissions { get; set; }
public ServerPermission BadPermissions { get; set; }
private Role? Parent = null;
public required PublicServer Server { get; init; }
public Task<Role> GetRole()
public async Task<Role> GetRole()
{
if (Parent is null)
{
}
return Task.FromResult(Parent)!;
return await Server.GetRole(ParentRoleID);
}
}

View File

@ -16,7 +16,7 @@ public class SocketAppUser : SocketUser
return await Server.GetChannel<SocketChannel>(SelectedChannel, Token);
}
public async Task<bool> HasAccessToCategory(SocketCategory Category, ServerPermission RequiredPerms)
public async Task<bool> HasAccessToCategory(SocketCategory Category, ServerPermission RequiredPerms = ServerPermission.ViewThis)
{
if (Category.Server != Server) return false;
if (Server.OwnerID == Id) return true;
@ -33,18 +33,29 @@ public class SocketAppUser : SocketUser
GoodPerms |= CatUserOveride.GoodPermissions;
}
RoleOveride[] CatRoleOverides = await Category.GetRoleOverrides();
foreach (RoleOveride CatRoleOveride in CatRoleOverides)
RoleOverride[] CatRoleOverides = await Category.GetRoleOverrides();
int bad_index = -1;
int good_index = -1;
foreach (RoleOverride CatRoleOveride in CatRoleOverides)
{
if (!RoleIds.Contains(CatRoleOveride.ParentRoleID)) continue;
if ((CatRoleOveride.BadPermissions & RequiredPerms) > ServerPermission.None) return false;
var index = (await CatRoleOveride.GetRole()).Index;
if ((CatRoleOveride.BadPermissions & RequiredPerms) > ServerPermission.None)
{
if (bad_index < index)
bad_index = index;
}
else good_index = index;
GoodPerms |= CatRoleOveride.GoodPermissions;
}
if (bad_index > good_index) return false;
foreach (Role RoleID in UserRoleIDList)
{
if (((RoleID.ServerPermissions & RequiredPerms) ^ GoodPerms) > ServerPermission.None) return false;
GoodPerms |= RoleID.ServerPermissions;
}
return GoodPerms.HasPermission(RequiredPerms);

View File

@ -17,7 +17,7 @@ public class SocketCategory
internal long[] Channels { get; set; }
internal long[] Categories { get; set; }
SocketCategory? RawParent = null;
List<RoleOveride>? RawRoleOverides = null;
List<RoleOverride>? RawRoleOverides = null;
List<UserOverride>? RawUserOverides = null;
List<SocketChannel>? RawChan = null;
List<SocketCategory>? RawCat = null;
@ -79,7 +79,7 @@ public class SocketCategory
public string Name { get; internal set; }
public string Description { get; internal set; }
public Task<RoleOveride[]> GetRoleOverrides()
public Task<RoleOverride[]> GetRoleOverrides()
{
if (RawRoleOverides is null)
{

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using JacobTechEncryption;
@ -20,7 +21,7 @@ public class SocketChannel
internal long[] RoleOverrides { get; set; }
internal long[] UserOverrides { get; set; }
SocketCategory? RawParent = null;
List<RoleOveride>? RawRoleOverides = null;
List<RoleOverride>? RawRoleOverides = null;
List<UserOverride>? RawUserOverides = null;
public PictureType PictureType { get; internal set; }
@ -336,16 +337,20 @@ public class SocketChannel
public DateTime Epoch { get; internal set; }
public string Name { get; internal set; }
public string Description { get; internal set; }
public Task<RoleOveride[]> GetRoleOverides()
public async Task<RoleOverride[]> GetRoleOverrides()
{
if (RawRoleOverides is null)
{
RawRoleOverides = new();
foreach (long ro in RoleOverrides)
{
RawRoleOverides.Add(await Server.GetRoleOverride(ro));
}
}
return Task.FromResult(RawRoleOverides!.ToArray());
return RawRoleOverides!.ToArray();
}
public Task<UserOverride[]> GetUserOveride()
public Task<UserOverride[]> GetUserOverride()
{
if (RawUserOverides is null)
{
@ -354,6 +359,65 @@ public class SocketChannel
return Task.FromResult(RawUserOverides!.ToArray());
}
public async Task<SocketUser[]> GetMembers()
{
ServerPermission req = ServerPermission.ViewThis;
List<long> GoodMembers = new();
List<long> GoodRoles = new();
List<long> BadRoles = new();
List<SocketUser> GoodPeople = new();
foreach (UserOverride cro in await GetUserOverride())
{
if ((cro.GoodPermissions & req) == req) GoodMembers.Add(cro.UserID);
}
foreach (RoleOverride ro in (await GetRoleOverrides()))
{
if ((ro.GoodPermissions & req) == req)
{
GoodRoles.Add(ro.ParentRoleID);
}
else if ((ro.BadPermissions & req) == req)
{
BadRoles.Add(ro.ParentRoleID);
}
}
bool bad = false;
foreach (Role Role in Server.roles.OrderBy(s => s.Index))
{
if (BadRoles.Contains(Role.ID))
{
bad = true;
}
if (bad && GoodRoles.Contains(Role.ID))
{
bad = false;
}
if (!bad)
{
foreach (var m in await Role.GetMembers())
{
var t = GoodPeople.Where(s => s.Id == m.Id);
if (t.Count() == 0) GoodPeople.Add(m);
}
}
}
foreach (long m in GoodMembers)
{
var t = GoodPeople.Where(s => s.Id == m);
if (t.Count() == 0) GoodPeople.Add(await Server.GetUser<SocketUser>(m, CancellationToken.None));
}
return GoodPeople.ToArray();
}
public long TitleEncryptionKey { get; internal set; }
public long DescriptionEncryptionKey { get; internal set; }
public long[] EncryptionKeys { get; internal set; }