From 7968fc20a56636937ef092005e62f5875ede4b50 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Mon, 13 Nov 2023 23:37:04 -0600 Subject: [PATCH] add user role and editable --- .../Services/AgentService.CreateAgent.cs | 1 + .../Services/AgentService.RefreshAgents.cs | 1 + .../Repository/FileRepository.cs | 23 +++++++++++++------ .../ViewModels/Users/UserCreationModel.cs | 5 +++- .../Collections/UserAgentCollection.cs | 1 + .../Collections/UserCollection.cs | 1 + .../Repository/MongoRepository.cs | 12 ++++++++-- 7 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs index 2253710c..be9ef658 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs @@ -48,6 +48,7 @@ public partial class AgentService Id = Guid.NewGuid().ToString(), UserId = user.Id, AgentId = foundAgent?.Id ?? agentRecord.Id, + Editable = false, CreatedTime = DateTime.UtcNow, UpdatedTime = DateTime.UtcNow }; diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs index 7d0d4ced..5deb6ebd 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs @@ -39,6 +39,7 @@ public partial class AgentService Id = Guid.NewGuid().ToString(), UserId = user.Id, AgentId = agent.Id, + Editable = false, CreatedTime = DateTime.UtcNow, UpdatedTime = DateTime.UtcNow }; diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index e77e160a..02d2b1c6 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -52,7 +52,11 @@ public class FileRepository : IBotSharpRepository { foreach (var d in Directory.GetDirectories(dir)) { - var json = File.ReadAllText(Path.Combine(d, "user.json")); + var userFile = Path.Combine(d, "user.json"); + if (!Directory.Exists(d) || !File.Exists(userFile)) + continue; + + var json = File.ReadAllText(userFile); _users.Add(JsonSerializer.Deserialize(json, _options)); } } @@ -75,7 +79,11 @@ public class FileRepository : IBotSharpRepository { foreach (var d in Directory.GetDirectories(dir)) { - var json = File.ReadAllText(Path.Combine(d, "agent.json")); + var file = Path.Combine(d, "agent.json"); + if (!Directory.Exists(d) || !File.Exists(file)) + continue; + + var json = File.ReadAllText(file); var agent = JsonSerializer.Deserialize(json, _options); if (agent != null) { @@ -108,11 +116,11 @@ public class FileRepository : IBotSharpRepository foreach (var d in Directory.GetDirectories(dir)) { var file = Path.Combine(d, "agents.json"); - if (Directory.Exists(d) && File.Exists(file)) - { - var json = File.ReadAllText(file); - _userAgents.AddRange(JsonSerializer.Deserialize>(json, _options)); - } + if (!Directory.Exists(d) || !File.Exists(file)) + continue; + + var json = File.ReadAllText(file); + _userAgents.AddRange(JsonSerializer.Deserialize>(json, _options)); } } return _userAgents.AsQueryable(); @@ -777,6 +785,7 @@ public class FileRepository : IBotSharpRepository public void CreateUser(User user) { var userId = Guid.NewGuid().ToString(); + user.Id = userId; var dir = Path.Combine(_dbSettings.FileRepository, "users", userId); if (!Directory.Exists(dir)) { diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserCreationModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserCreationModel.cs index 77fc69c5..f72749bf 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserCreationModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserCreationModel.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Users.Enums; using BotSharp.Abstraction.Users.Models; namespace BotSharp.OpenAPI.ViewModels.Users; @@ -8,6 +9,7 @@ public class UserCreationModel public string LastName { get; set; } = string.Empty; public string Email { get; set; } = string.Empty; public string Password { get; set; } = string.Empty; + public string Role { get; set; } = UserRole.Client; public User ToUser() { @@ -16,7 +18,8 @@ public class UserCreationModel FirstName = FirstName, LastName = LastName, Email = Email, - Password = Password + Password = Password, + Role = Role }; } } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserAgentCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserAgentCollection.cs index 3e9f4043..2142ad42 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserAgentCollection.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserAgentCollection.cs @@ -4,6 +4,7 @@ public class UserAgentCollection : MongoBase { public string UserId { get; set; } public string AgentId { get; set; } + public bool Editable { get; set; } public DateTime CreatedTime { get; set; } public DateTime UpdatedTime { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserCollection.cs index 03327410..5220dd87 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserCollection.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserCollection.cs @@ -8,6 +8,7 @@ public class UserCollection : MongoBase public string Salt { get; set; } public string Password { get; set; } public string? ExternalId { get; set; } + public string Role { get; set; } public DateTime CreatedTime { get; set; } public DateTime UpdatedTime { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs index aacd623b..92e13b0a 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -148,6 +148,7 @@ public class MongoRepository : IBotSharpRepository Password = x.Password, Email = x.Email, ExternalId = x.ExternalId, + Role = x.Role, CreatedTime = x.CreatedTime, UpdatedTime = x.UpdatedTime }).ToList(); @@ -162,6 +163,7 @@ public class MongoRepository : IBotSharpRepository .Set(x => x.Salt, user.Salt) .Set(x => x.Password, user.Password) .Set(x => x.ExternalId, user.ExternalId) + .Set(x => x.Role, user.Role) .Set(x => x.CreatedTime, user.CreatedTime) .Set(x => x.UpdatedTime, user.UpdatedTime); _dc.Users.UpdateOne(filter, update, _options); @@ -174,6 +176,7 @@ public class MongoRepository : IBotSharpRepository Id = !string.IsNullOrEmpty(x.Id) ? x.Id : Guid.NewGuid().ToString(), AgentId = x.AgentId, UserId = !string.IsNullOrEmpty(x.UserId) ? x.UserId : string.Empty, + Editable = x.Editable, CreatedTime = x.CreatedTime, UpdatedTime = x.UpdatedTime }).ToList(); @@ -184,6 +187,7 @@ public class MongoRepository : IBotSharpRepository var update = Builders.Update .Set(x => x.AgentId, userAgent.AgentId) .Set(x => x.UserId, userAgent.UserId) + .Set(x => x.Editable, userAgent.Editable) .Set(x => x.CreatedTime, userAgent.CreatedTime) .Set(x => x.UpdatedTime, userAgent.UpdatedTime); _dc.UserAgents.UpdateOne(filter, update, _options); @@ -571,6 +575,7 @@ public class MongoRepository : IBotSharpRepository Id = !string.IsNullOrEmpty(x.Id) ? x.Id : Guid.NewGuid().ToString(), AgentId = x.AgentId, UserId = !string.IsNullOrEmpty(x.UserId) ? x.UserId : string.Empty, + Editable = x.Editable, CreatedTime = x.CreatedTime, UpdatedTime = x.UpdatedTime }).ToList(); @@ -764,7 +769,8 @@ public class MongoRepository : IBotSharpRepository Email = user.Email, Password = user.Password, Salt = user.Salt, - ExternalId = user.ExternalId + ExternalId = user.ExternalId, + Role = user.Role } : null; } @@ -779,7 +785,8 @@ public class MongoRepository : IBotSharpRepository Email = user.Email, Password = user.Password, Salt = user.Salt, - ExternalId = user.ExternalId + ExternalId = user.ExternalId, + Role = user.Role } : null; } @@ -796,6 +803,7 @@ public class MongoRepository : IBotSharpRepository Password = user.Password, Email = user.Email, ExternalId = user.ExternalId, + Role = user.Role, CreatedTime = DateTime.UtcNow, UpdatedTime = DateTime.UtcNow };