Skip to content

Commit 60d24e1

Browse files
authored
Merge pull request #208 from iceljc/features/add-user-role
add user role and editable
2 parents 5ca1203 + 7968fc2 commit 60d24e1

File tree

7 files changed

+34
-10
lines changed

7 files changed

+34
-10
lines changed

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public async Task<Agent> CreateAgent(Agent agent)
4848
Id = Guid.NewGuid().ToString(),
4949
UserId = user.Id,
5050
AgentId = foundAgent?.Id ?? agentRecord.Id,
51+
Editable = false,
5152
CreatedTime = DateTime.UtcNow,
5253
UpdatedTime = DateTime.UtcNow
5354
};

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public async Task RefreshAgents()
3939
Id = Guid.NewGuid().ToString(),
4040
UserId = user.Id,
4141
AgentId = agent.Id,
42+
Editable = false,
4243
CreatedTime = DateTime.UtcNow,
4344
UpdatedTime = DateTime.UtcNow
4445
};

src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ private IQueryable<User> Users
5252
{
5353
foreach (var d in Directory.GetDirectories(dir))
5454
{
55-
var json = File.ReadAllText(Path.Combine(d, "user.json"));
55+
var userFile = Path.Combine(d, "user.json");
56+
if (!Directory.Exists(d) || !File.Exists(userFile))
57+
continue;
58+
59+
var json = File.ReadAllText(userFile);
5660
_users.Add(JsonSerializer.Deserialize<User>(json, _options));
5761
}
5862
}
@@ -75,7 +79,11 @@ private IQueryable<Agent> Agents
7579
{
7680
foreach (var d in Directory.GetDirectories(dir))
7781
{
78-
var json = File.ReadAllText(Path.Combine(d, "agent.json"));
82+
var file = Path.Combine(d, "agent.json");
83+
if (!Directory.Exists(d) || !File.Exists(file))
84+
continue;
85+
86+
var json = File.ReadAllText(file);
7987
var agent = JsonSerializer.Deserialize<Agent>(json, _options);
8088
if (agent != null)
8189
{
@@ -108,11 +116,11 @@ private IQueryable<UserAgent> UserAgents
108116
foreach (var d in Directory.GetDirectories(dir))
109117
{
110118
var file = Path.Combine(d, "agents.json");
111-
if (Directory.Exists(d) && File.Exists(file))
112-
{
113-
var json = File.ReadAllText(file);
114-
_userAgents.AddRange(JsonSerializer.Deserialize<List<UserAgent>>(json, _options));
115-
}
119+
if (!Directory.Exists(d) || !File.Exists(file))
120+
continue;
121+
122+
var json = File.ReadAllText(file);
123+
_userAgents.AddRange(JsonSerializer.Deserialize<List<UserAgent>>(json, _options));
116124
}
117125
}
118126
return _userAgents.AsQueryable();
@@ -777,6 +785,7 @@ public List<string> GetExectionLogs(string conversationId)
777785
public void CreateUser(User user)
778786
{
779787
var userId = Guid.NewGuid().ToString();
788+
user.Id = userId;
780789
var dir = Path.Combine(_dbSettings.FileRepository, "users", userId);
781790
if (!Directory.Exists(dir))
782791
{

src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserCreationModel.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Users.Enums;
12
using BotSharp.Abstraction.Users.Models;
23

34
namespace BotSharp.OpenAPI.ViewModels.Users;
@@ -8,6 +9,7 @@ public class UserCreationModel
89
public string LastName { get; set; } = string.Empty;
910
public string Email { get; set; } = string.Empty;
1011
public string Password { get; set; } = string.Empty;
12+
public string Role { get; set; } = UserRole.Client;
1113

1214
public User ToUser()
1315
{
@@ -16,7 +18,8 @@ public User ToUser()
1618
FirstName = FirstName,
1719
LastName = LastName,
1820
Email = Email,
19-
Password = Password
21+
Password = Password,
22+
Role = Role
2023
};
2124
}
2225
}

src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserAgentCollection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public class UserAgentCollection : MongoBase
44
{
55
public string UserId { get; set; }
66
public string AgentId { get; set; }
7+
public bool Editable { get; set; }
78

89
public DateTime CreatedTime { get; set; }
910
public DateTime UpdatedTime { get; set; }

src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserCollection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class UserCollection : MongoBase
88
public string Salt { get; set; }
99
public string Password { get; set; }
1010
public string? ExternalId { get; set; }
11+
public string Role { get; set; }
1112

1213
public DateTime CreatedTime { get; set; }
1314
public DateTime UpdatedTime { get; set; }

src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public int Transaction<TTableInterface>(Action action)
148148
Password = x.Password,
149149
Email = x.Email,
150150
ExternalId = x.ExternalId,
151+
Role = x.Role,
151152
CreatedTime = x.CreatedTime,
152153
UpdatedTime = x.UpdatedTime
153154
}).ToList();
@@ -162,6 +163,7 @@ public int Transaction<TTableInterface>(Action action)
162163
.Set(x => x.Salt, user.Salt)
163164
.Set(x => x.Password, user.Password)
164165
.Set(x => x.ExternalId, user.ExternalId)
166+
.Set(x => x.Role, user.Role)
165167
.Set(x => x.CreatedTime, user.CreatedTime)
166168
.Set(x => x.UpdatedTime, user.UpdatedTime);
167169
_dc.Users.UpdateOne(filter, update, _options);
@@ -174,6 +176,7 @@ public int Transaction<TTableInterface>(Action action)
174176
Id = !string.IsNullOrEmpty(x.Id) ? x.Id : Guid.NewGuid().ToString(),
175177
AgentId = x.AgentId,
176178
UserId = !string.IsNullOrEmpty(x.UserId) ? x.UserId : string.Empty,
179+
Editable = x.Editable,
177180
CreatedTime = x.CreatedTime,
178181
UpdatedTime = x.UpdatedTime
179182
}).ToList();
@@ -184,6 +187,7 @@ public int Transaction<TTableInterface>(Action action)
184187
var update = Builders<UserAgentCollection>.Update
185188
.Set(x => x.AgentId, userAgent.AgentId)
186189
.Set(x => x.UserId, userAgent.UserId)
190+
.Set(x => x.Editable, userAgent.Editable)
187191
.Set(x => x.CreatedTime, userAgent.CreatedTime)
188192
.Set(x => x.UpdatedTime, userAgent.UpdatedTime);
189193
_dc.UserAgents.UpdateOne(filter, update, _options);
@@ -571,6 +575,7 @@ public void BulkInsertUserAgents(List<UserAgent> userAgents)
571575
Id = !string.IsNullOrEmpty(x.Id) ? x.Id : Guid.NewGuid().ToString(),
572576
AgentId = x.AgentId,
573577
UserId = !string.IsNullOrEmpty(x.UserId) ? x.UserId : string.Empty,
578+
Editable = x.Editable,
574579
CreatedTime = x.CreatedTime,
575580
UpdatedTime = x.UpdatedTime
576581
}).ToList();
@@ -764,7 +769,8 @@ public List<string> GetExectionLogs(string conversationId)
764769
Email = user.Email,
765770
Password = user.Password,
766771
Salt = user.Salt,
767-
ExternalId = user.ExternalId
772+
ExternalId = user.ExternalId,
773+
Role = user.Role
768774
} : null;
769775
}
770776

@@ -779,7 +785,8 @@ public List<string> GetExectionLogs(string conversationId)
779785
Email = user.Email,
780786
Password = user.Password,
781787
Salt = user.Salt,
782-
ExternalId = user.ExternalId
788+
ExternalId = user.ExternalId,
789+
Role = user.Role
783790
} : null;
784791
}
785792

@@ -796,6 +803,7 @@ public void CreateUser(User user)
796803
Password = user.Password,
797804
Email = user.Email,
798805
ExternalId = user.ExternalId,
806+
Role = user.Role,
799807
CreatedTime = DateTime.UtcNow,
800808
UpdatedTime = DateTime.UtcNow
801809
};

0 commit comments

Comments
 (0)