Merge pull request #208 from iceljc/features/add-user-role

add user role and editable
This commit is contained in:
Haiping 2023-11-14 06:30:16 -06:00 committed by GitHub
commit 60d24e1cc6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 34 additions and 10 deletions

View file

@ -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
};

View file

@ -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
};

View file

@ -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<User>(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<Agent>(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<List<UserAgent>>(json, _options));
}
if (!Directory.Exists(d) || !File.Exists(file))
continue;
var json = File.ReadAllText(file);
_userAgents.AddRange(JsonSerializer.Deserialize<List<UserAgent>>(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))
{

View file

@ -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
};
}
}

View file

@ -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; }

View file

@ -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; }

View file

@ -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<UserAgentCollection>.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
};