add update user
This commit is contained in:
parent
170d9ffc1e
commit
d0b7e08cfe
|
|
@ -33,6 +33,7 @@ public interface IBotSharpRepository
|
|||
void UpdateUserIsDisable(string userId, bool isDisable) => throw new NotImplementedException();
|
||||
void UpdateUsersIsDisable(List<string> userIds, bool isDisable) => throw new NotImplementedException();
|
||||
PagedItems<User> GetUsers(UserFilter filter) => throw new NotImplementedException();
|
||||
bool UpdateUser(User user, bool isUpdateUserAgents = false) => throw new NotImplementedException();
|
||||
#endregion
|
||||
|
||||
#region Agent
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ public interface IUserService
|
|||
{
|
||||
Task<User> GetUser(string id);
|
||||
Task<PagedItems<User>> GetUsers(UserFilter filter);
|
||||
Task<bool> UpdateUser(User model, bool isUpdateUserAgents = false);
|
||||
Task<User> CreateUser(User user);
|
||||
Task<Token> ActiveUser(UserActivationModel model);
|
||||
Task<Token?> GetAffiliateToken(string authorization);
|
||||
|
|
|
|||
|
|
@ -449,9 +449,15 @@ namespace BotSharp.Core.Repository
|
|||
return true;
|
||||
}
|
||||
|
||||
public void BulkInsertAgents(List<Agent> agents) { }
|
||||
public void BulkInsertAgents(List<Agent> agents)
|
||||
{
|
||||
_agents = [];
|
||||
}
|
||||
|
||||
public void BulkInsertUserAgents(List<UserAgent> userAgents) { }
|
||||
public void BulkInsertUserAgents(List<UserAgent> userAgents)
|
||||
{
|
||||
_userAgents = [];
|
||||
}
|
||||
|
||||
public bool DeleteAgents()
|
||||
{
|
||||
|
|
@ -487,6 +493,8 @@ namespace BotSharp.Core.Repository
|
|||
|
||||
// Delete agent folder
|
||||
Directory.Delete(agentDir, true);
|
||||
_agents = [];
|
||||
_userAgents = [];
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Users.Enums;
|
||||
using BotSharp.Abstraction.Users.Models;
|
||||
using System;
|
||||
|
|
@ -130,4 +131,39 @@ public partial class FileRepository
|
|||
Count = users.Count()
|
||||
};
|
||||
}
|
||||
|
||||
public bool UpdateUser(User user, bool isUpdateUserAgents = false)
|
||||
{
|
||||
if (string.IsNullOrEmpty(user?.Id)) return false;
|
||||
|
||||
var dir = Path.Combine(_dbSettings.FileRepository, USERS_FOLDER, user.Id);
|
||||
if (!Directory.Exists(dir))
|
||||
{
|
||||
Directory.CreateDirectory(dir);
|
||||
}
|
||||
|
||||
var userFile = Path.Combine(dir, USER_FILE);
|
||||
user.UpdatedTime = DateTime.UtcNow;
|
||||
File.WriteAllText(_dbSettings.FileRepository, JsonSerializer.Serialize(user, _options));
|
||||
|
||||
if (isUpdateUserAgents)
|
||||
{
|
||||
var userAgents = user.AgentActions?.Select(x => new UserAgent
|
||||
{
|
||||
Id = !string.IsNullOrEmpty(x.Id) ? x.Id : Guid.NewGuid().ToString(),
|
||||
UserId = user.Id,
|
||||
AgentId = x.AgentId,
|
||||
Actions = x.Actions ?? [],
|
||||
CreatedTime = DateTime.UtcNow,
|
||||
UpdatedTime = DateTime.UtcNow
|
||||
})?.ToList() ?? [];
|
||||
|
||||
var userAgentFile = Path.Combine(dir, USER_AGENT_FILE);
|
||||
File.WriteAllText(userAgentFile, JsonSerializer.Serialize(userAgents, _options));
|
||||
}
|
||||
|
||||
_users = [];
|
||||
_userAgents = [];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -384,6 +384,12 @@ public class UserService : IUserService
|
|||
return users;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateUser(User model, bool isUpdateUserAgents = false)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
return db.UpdateUser(model, isUpdateUserAgents);
|
||||
}
|
||||
|
||||
public async Task<Token> ActiveUser(UserActivationModel model)
|
||||
{
|
||||
var id = model.UserName;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using BotSharp.Abstraction.Users.Enums;
|
||||
using EntityFrameworkCore.BootKit;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
|
@ -192,6 +191,23 @@ public class UserController : ControllerBase
|
|||
Items = views
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
[HttpPut("/user")]
|
||||
public async Task<bool> UpdateUser([FromBody] UserUpdateModel model)
|
||||
{
|
||||
if (model == null) return false;
|
||||
|
||||
var userService = _services.GetRequiredService<IUserService>();
|
||||
var user = await userService.GetUser(_user.Id);
|
||||
if (user == null || !UserConstant.AdminRoles.Contains(user.Role))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var updated = await userService.UpdateUser(UserUpdateModel.ToUser(model), isUpdateUserAgents: true);
|
||||
return updated;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -27,4 +27,14 @@ public class UserAgentActionViewModel
|
|||
Actions = action.Actions
|
||||
};
|
||||
}
|
||||
|
||||
public static UserAgentAction ToDomainModel(UserAgentActionViewModel action)
|
||||
{
|
||||
return new UserAgentAction
|
||||
{
|
||||
Id = action.Id,
|
||||
AgentId = action.AgentId,
|
||||
Actions = action.Actions
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BotSharp.OpenAPI.ViewModels.Users;
|
||||
|
||||
public class UserUpdateModel
|
||||
{
|
||||
public string Id { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("user_name")]
|
||||
public string UserName { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("first_name")]
|
||||
public string FirstName { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("last_name")]
|
||||
public string? LastName { get; set; }
|
||||
public string? Email { get; set; }
|
||||
public string? Phone { get; set; }
|
||||
public string? Type { get; set; }
|
||||
public string? Role { get; set; }
|
||||
public string? Source { get; set; }
|
||||
|
||||
[JsonPropertyName("external_id")]
|
||||
public string? ExternalId { get; set; }
|
||||
|
||||
public IEnumerable<string> Permissions { get; set; } = [];
|
||||
|
||||
[JsonPropertyName("agent_actions")]
|
||||
public IEnumerable<UserAgentActionViewModel> AgentActions { get; set; } = [];
|
||||
|
||||
public static User ToUser(UserUpdateModel model)
|
||||
{
|
||||
return new User
|
||||
{
|
||||
Id = model.Id,
|
||||
UserName = model.UserName,
|
||||
FirstName = model.FirstName,
|
||||
LastName = model.LastName,
|
||||
Email = model.Email,
|
||||
Phone = model.Phone,
|
||||
Type = model.Type,
|
||||
Role = model.Role,
|
||||
Source = model.Source,
|
||||
ExternalId = model.ExternalId,
|
||||
Permissions = model.Permissions,
|
||||
AgentActions = model.AgentActions?.Select(x => UserAgentActionViewModel.ToDomainModel(x)) ?? []
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,7 @@
|
|||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Conversations.Models;
|
||||
using BotSharp.Abstraction.Repositories.Filters;
|
||||
using BotSharp.Abstraction.Users.Enums;
|
||||
using BotSharp.Abstraction.Users.Models;
|
||||
using System.Globalization;
|
||||
|
||||
namespace BotSharp.Plugin.MongoStorage.Repository;
|
||||
|
||||
|
|
@ -231,4 +229,46 @@ public partial class MongoRepository
|
|||
Count = (int)count
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public bool UpdateUser(User user, bool isUpdateUserAgents = false)
|
||||
{
|
||||
if (string.IsNullOrEmpty(user?.Id)) return false;
|
||||
|
||||
var userFilter = Builders<UserDocument>.Filter.Eq(x => x.Id, user.Id);
|
||||
var userUpdate = Builders<UserDocument>.Update
|
||||
.Set(x => x.Role, user.Role)
|
||||
.Set(x => x.UpdatedTime, DateTime.UtcNow);
|
||||
|
||||
_dc.Users.UpdateOne(userFilter, userUpdate);
|
||||
|
||||
if (isUpdateUserAgents)
|
||||
{
|
||||
var userAgentDocs = user.AgentActions?.Select(x => new UserAgentDocument
|
||||
{
|
||||
Id = !string.IsNullOrEmpty(x.Id) ? x.Id : Guid.NewGuid().ToString(),
|
||||
UserId = user.Id,
|
||||
AgentId = x.AgentId,
|
||||
Actions = x.Actions,
|
||||
CreatedTime = DateTime.UtcNow,
|
||||
UpdatedTime = DateTime.UtcNow
|
||||
})?.ToList() ?? [];
|
||||
|
||||
_dc.UserAgents.DeleteMany(Builders<UserAgentDocument>.Filter.Nin(x => x.Id, userAgentDocs.Select(x => x.Id)));
|
||||
foreach (var doc in userAgentDocs)
|
||||
{
|
||||
var userAgentFilter = Builders<UserAgentDocument>.Filter.Eq(x => x.Id, doc.Id);
|
||||
var userAgentUpdate = Builders<UserAgentDocument>.Update
|
||||
.Set(x => x.Id, doc.Id)
|
||||
.Set(x => x.UserId, user.Id)
|
||||
.Set(x => x.AgentId, doc.AgentId)
|
||||
.Set(x => x.Actions, doc.Actions)
|
||||
.Set(x => x.UpdatedTime, DateTime.UtcNow);
|
||||
|
||||
_dc.UserAgents.UpdateOne(userAgentFilter, userAgentUpdate, _options);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue