Skip to content

Commit 88903c2

Browse files
authored
Expand permissions system (#1267)
* Add call to ChangePermission * Move permissions system to another assembly * Add bool to toggle set owner permission on server host * Fix issues with merge * Fixed merge issues and added documentation to permission system * Fix missing import * Fix build error
1 parent 6bdf8ad commit 88903c2

40 files changed

+168
-83
lines changed
File renamed without changes.

Assets/Scripts/SS3D/Systems/Permissions/DisableIfNotAdmin.cs renamed to Assets/Scripts/SS3D/Permissions/DisableIfNotAdmin.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
31
using Coimbra;
42
using Coimbra.Services.Events;
5-
using Cysharp.Threading.Tasks;
63
using SS3D.Core;
74
using SS3D.Core.Behaviours;
85
using SS3D.Core.Settings;
9-
using SS3D.Core.Utils;
10-
using SS3D.Systems.Permissions.Events;
6+
using System.Collections.Generic;
7+
using System.Linq;
118
using UnityEngine;
9+
using UserPermissionsChangedEvent = SS3D.Permissions.Events.UserPermissionsChangedEvent;
1210

13-
namespace SS3D.Systems.Permissions
11+
namespace SS3D.Permissions
1412
{
1513
public class DisableIfNotAdmin : NetworkActor
1614
{
File renamed without changes.
File renamed without changes.

Assets/Scripts/SS3D/Systems/Permissions/Events/UserPermissionsChangedEvent.cs renamed to Assets/Scripts/SS3D/Permissions/Events/UserPermissionsChangedEvent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System.Collections.Generic;
2-
using Coimbra.Services.Events;
1+
using Coimbra.Services.Events;
2+
using System.Collections.Generic;
33

4-
namespace SS3D.Systems.Permissions.Events
4+
namespace SS3D.Permissions.Events
55
{
66
public partial struct UserPermissionsChangedEvent : IEvent
77
{
File renamed without changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Coimbra;
2+
using UnityEngine;
3+
4+
namespace SS3D.Permissions
5+
{
6+
/// <summary>
7+
/// This settings has general options for the permission system, so if we add a database/SQL/API support we can set stuff here.
8+
/// </summary>
9+
[ProjectSettings("SS3D/Server")]
10+
public class PermissionSettings : ScriptableSettings
11+
{
12+
[SerializeField]
13+
private bool _addServerOwnerPermissionToServerHost;
14+
15+
/// <summary>
16+
/// We can define if the host will get the owner permission when he joins the game.
17+
/// </summary>
18+
public static bool AddServerOwnerPermissionToServerHost => GetOrFind<PermissionSettings>()._addServerOwnerPermissionToServerHost;
19+
}
20+
}

Assets/Scripts/SS3D/Permissions/PermissionSettings.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/SS3D/Systems/Permissions/PermissionSystem.cs renamed to Assets/Scripts/SS3D/Permissions/PermissionSystem.cs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using FishNet.Object;
1+
using FishNet.Object;
52
using FishNet.Object.Synchronizing;
63
using SS3D.Core.Behaviours;
74
using SS3D.Data;
85
using SS3D.Logging;
9-
using SS3D.Systems.Permissions.Events;
6+
using SS3D.Permissions.Events;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
1010
using File = System.IO.File;
11+
using UserPermissionsChangedEvent = SS3D.Permissions.Events.UserPermissionsChangedEvent;
1112

12-
namespace SS3D.Systems.Permissions
13+
namespace SS3D.Permissions
1314
{
1415
/// <summary>
1516
/// Handles user permission on what he can do and can't.
@@ -87,15 +88,33 @@ public bool TryGetUserRole(string ckey, out ServerRoleTypes userPermission)
8788
/// <summary>
8889
/// Updates a user permission.
8990
/// </summary>
90-
/// <param name="ckey">The desired user to update the permission.</param>
91+
/// <param name="ckey">The desired user to update the permission.</param>|
9192
/// <param name="role">The new user role.</param>
9293
[Server]
9394
public void ChangeUserPermission(string ckey, ServerRoleTypes role)
9495
{
95-
throw new NotImplementedException();
96-
// TODO: This
97-
// Add new user permission to list
98-
// Add new user permission to text file
96+
ServerRoleTypes previousRole = _userPermissions.TryGetValue(ckey, out ServerRoleTypes permission) ? permission : ServerRoleTypes.None;
97+
98+
Log.Information(this, $"Updating user {ckey} role from {previousRole} to {role}");
99+
100+
_userPermissions[ckey] = role;
101+
102+
SaveUserPermissions();
103+
}
104+
105+
/// <summary>
106+
/// Saves the permissions text file with the updated permissions list.
107+
/// </summary>
108+
public void SaveUserPermissions()
109+
{
110+
string fileContent = string.Empty;
111+
112+
foreach (KeyValuePair<string,ServerRoleTypes> userPermission in _userPermissions)
113+
{
114+
fileContent += $"{userPermission.Key} {userPermission.Value.ToString()}\n";
115+
}
116+
117+
File.WriteAllText(PermissionsPath, fileContent);
99118
}
100119

101120
/// <summary>
@@ -164,6 +183,12 @@ private void SyncHandleUserPermissionsChanged(SyncDictionaryOperation op, string
164183
SyncUserPermissions();
165184
}
166185

186+
/// <summary>
187+
/// Returns if the user is at least at a level of access.
188+
/// </summary>
189+
/// <param name="ckey">The user to check permission for</param>
190+
/// <param name="permissionLevelCheck">The lowest required permission to perform the action.</param>
191+
/// <returns></returns>
167192
public bool IsAtLeast(string ckey, ServerRoleTypes permissionLevelCheck)
168193
{
169194
TryGetUserRole(ckey, out ServerRoleTypes userPermission);
File renamed without changes.

0 commit comments

Comments
 (0)