From 1f84b7d7da1464fab8178188f97164e4718527ed Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 15 Dec 2020 11:02:54 +0200 Subject: Updated role controller and role service to use custom models and implemented role mapping --- src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs (limited to 'src/DevHive.Services/Models/Identity/Role') diff --git a/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs new file mode 100644 index 0000000..3f834ef --- /dev/null +++ b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Services.Models.Identity.Role +{ + public class RoleServiceModel + { + public Guid Id { get; set; } + public string Name { get; set; } + } +} -- cgit v1.2.3 From 33a1a5899a16378691cd62d9ee4644db2a02e2b7 Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 17 Dec 2020 19:37:38 +0200 Subject: RoleModel moved to DevHive.Common --- src/DevHive.Common/Models/Identity/RoleModel.cs | 10 ++++++++++ src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs | 10 ---------- src/DevHive.Services/Models/Identity/User/UserServiceModel.cs | 3 ++- .../Configurations/Extensions/ConfigureAutoMapper.cs | 9 +++++++++ src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs | 10 ---------- src/DevHive.Web/Models/Identity/User/UserWebModel.cs | 3 ++- src/DevHive.Web/Startup.cs | 2 +- 7 files changed, 24 insertions(+), 23 deletions(-) create mode 100644 src/DevHive.Common/Models/Identity/RoleModel.cs delete mode 100644 src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs delete mode 100644 src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs (limited to 'src/DevHive.Services/Models/Identity/Role') diff --git a/src/DevHive.Common/Models/Identity/RoleModel.cs b/src/DevHive.Common/Models/Identity/RoleModel.cs new file mode 100644 index 0000000..5db8df9 --- /dev/null +++ b/src/DevHive.Common/Models/Identity/RoleModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Common.Models.Identity +{ + public class RoleModel + { + public Guid Id { get; set; } + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs deleted file mode 100644 index 3f834ef..0000000 --- a/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Identity.Role -{ - public class RoleServiceModel - { - public Guid Id { get; set; } - public string Name { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 868e7ba..8ac71b1 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using DevHive.Common.Models.Identity; using DevHive.Data.Models; using DevHive.Services.Models.Identity.Role; @@ -6,7 +7,7 @@ namespace DevHive.Services.Models.Identity.User { public class UserServiceModel : BaseUserServiceModel { - public IList Role { get; set; } = new List(); + public IList Role { get; set; } = new List(); public List Friends { get; set; } = new List(); } } diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs index afba39c..b6ebc63 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs @@ -1,6 +1,7 @@ using System; using AutoMapper; using AutoMapper.Configuration; +using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; namespace DevHive.Web.Configurations.Extensions @@ -11,5 +12,13 @@ namespace DevHive.Web.Configurations.Extensions { services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); } + + public static void UseAutoMapperConfiguration(this IApplicationBuilder app) + { + var config = new MapperConfiguration(cfg => + { + cfg.AllowNullCollections = true; + }); + } } } \ No newline at end of file diff --git a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs deleted file mode 100644 index d8ae465..0000000 --- a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace DevHive.Web.Models.Identity.Role -{ - public class RoleWebModel - { - public Guid Id { get; set; } - public string Name { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index e7bdb2a..83430ce 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -1,11 +1,12 @@ using System.Collections.Generic; +using DevHive.Common.Models.Identity; using DevHive.Web.Models.Identity.Role; namespace DevHive.Web.Models.Identity.User { public class UserWebModel : BaseUserWebModel { - public IList Role { get; set; } = new List(); + public IList Role { get; set; } = new List(); public IList Friends { get; set; } = new List(); } } diff --git a/src/DevHive.Web/Startup.cs b/src/DevHive.Web/Startup.cs index fac1b4a..66fde9e 100644 --- a/src/DevHive.Web/Startup.cs +++ b/src/DevHive.Web/Startup.cs @@ -50,8 +50,8 @@ namespace DevHive.Web } app.UseDatabaseConfiguration(); + app.UseAutoMapperConfiguration(); - app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( -- cgit v1.2.3 From 8d1d0b40d56f90248f948e474330258bf57cf0b6 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 17 Jan 2021 14:45:48 +0200 Subject: Fixed role implementation by bringing back and improving all role models --- src/DevHive.Common/Models/Identity/RoleModel.cs | 10 -------- .../Configurations/Mapping/RoleMapings.cs | 8 +++---- src/DevHive.Services/Interfaces/IRoleService.cs | 10 ++++---- .../Models/Identity/Role/RoleServiceModel.cs | 10 ++++++++ .../Models/Identity/Role/UpdateRoleServiceModel.cs | 7 ++++++ .../Models/Identity/User/UserServiceModel.cs | 6 ++--- src/DevHive.Services/Services/RoleService.cs | 24 +++++++++++++------ .../Configurations/Mapping/RoleMappings.cs | 12 +++++----- src/DevHive.Web/Controllers/RoleController.cs | 28 +++++++++++----------- .../Models/Identity/Role/CreateRoleWebModel.cs | 2 +- .../Models/Identity/Role/RoleWebModel.cs | 2 -- .../Models/Identity/Role/UpdateRoleWebModel.cs | 2 +- 12 files changed, 68 insertions(+), 53 deletions(-) delete mode 100644 src/DevHive.Common/Models/Identity/RoleModel.cs create mode 100644 src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs create mode 100644 src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs (limited to 'src/DevHive.Services/Models/Identity/Role') diff --git a/src/DevHive.Common/Models/Identity/RoleModel.cs b/src/DevHive.Common/Models/Identity/RoleModel.cs deleted file mode 100644 index 5db8df9..0000000 --- a/src/DevHive.Common/Models/Identity/RoleModel.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace DevHive.Common.Models.Identity -{ - public class RoleModel - { - public Guid Id { get; set; } - public string Name { get; set; } - } -} diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index 65b0b5a..4ddd253 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -1,15 +1,15 @@ using DevHive.Data.Models; using AutoMapper; -using DevHive.Common.Models.Identity; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Services.Configurations.Mapping { - public class RoleMappings : Profile + public class RoleMappings : Profile { public RoleMappings() { - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Interfaces/IRoleService.cs b/src/DevHive.Services/Interfaces/IRoleService.cs index 2c7195c..a7a0e47 100644 --- a/src/DevHive.Services/Interfaces/IRoleService.cs +++ b/src/DevHive.Services/Interfaces/IRoleService.cs @@ -1,16 +1,16 @@ using System; using System.Threading.Tasks; -using DevHive.Common.Models.Identity; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Services.Interfaces { - public interface IRoleService + public interface IRoleService { - Task CreateRole(RoleModel roleServiceModel); + Task CreateRole(RoleServiceModel roleServiceModel); - Task GetRoleById(Guid id); + Task GetRoleById(Guid id); - Task UpdateRole(RoleModel roleServiceModel); + Task UpdateRole(RoleServiceModel roleServiceModel); Task DeleteRole(Guid id); } diff --git a/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs new file mode 100644 index 0000000..3f834ef --- /dev/null +++ b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Services.Models.Identity.Role +{ + public class RoleServiceModel + { + public Guid Id { get; set; } + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs new file mode 100644 index 0000000..be71771 --- /dev/null +++ b/src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Services.Models.Identity.Role +{ + public class UpdateRoleServiceModel + { + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index ecf8c42..aa77a0c 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -1,13 +1,13 @@ using System.Collections.Generic; -using DevHive.Common.Models.Identity; +using DevHive.Services.Models.Identity.Role; using DevHive.Services.Models.Language; using DevHive.Services.Models.Technology; namespace DevHive.Services.Models.Identity.User { - public class UserServiceModel : BaseUserServiceModel + public class UserServiceModel : BaseUserServiceModel { - public IList Roles { get; set; } = new List(); + public IList Roles { get; set; } = new List(); public IList Friends { get; set; } = new List(); diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index c38ac74..0945624 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -1,14 +1,15 @@ using System; using System.Threading.Tasks; using AutoMapper; -using DevHive.Common.Models.Identity; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using DevHive.Services.Interfaces; +using DevHive.Services.Models.Identity.Role; +using DevHive.Services.Models.Language; namespace DevHive.Services.Services { - public class RoleService : IRoleService + public class RoleService : IRoleService { private readonly IRoleRepository _roleRepository; private readonly IMapper _roleMapper; @@ -19,25 +20,34 @@ namespace DevHive.Services.Services this._roleMapper = mapper; } - public async Task CreateRole(RoleModel roleServiceModel) + public async Task CreateRole(RoleServiceModel roleServiceModel) { if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) throw new ArgumentException("Role already exists!"); + Role role = this._roleMapper.Map(roleServiceModel); + bool success = await this._roleRepository.AddAsync(role); + + if(success) + { + Role newRole = await this._roleRepository.GetByNameAsync(roleServiceModel.Name); + return newRole.Id; + } + else + return Guid.Empty; - return await this._roleRepository.AddAsync(role); } - public async Task GetRoleById(Guid id) + public async Task GetRoleById(Guid id) { Role role = await this._roleRepository.GetByIdAsync(id) ?? throw new ArgumentException("Role does not exist!"); - return this._roleMapper.Map(role); + return this._roleMapper.Map(role); } - public async Task UpdateRole(RoleModel roleServiceModel) + public async Task UpdateRole(RoleServiceModel roleServiceModel) { if (!await this._roleRepository.DoesRoleExist(roleServiceModel.Id)) throw new ArgumentException("Role does not exist!"); diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index afa3a94..bce7c07 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -1,18 +1,18 @@ using AutoMapper; using DevHive.Web.Models.Identity.Role; -using DevHive.Common.Models.Identity; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Web.Configurations.Mapping { - public class RoleMappings : Profile + public class RoleMappings : Profile { public RoleMappings() { - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index 8ea2711..5b3dca5 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -3,12 +3,12 @@ using Microsoft.AspNetCore.Mvc; using DevHive.Web.Models.Identity.Role; using AutoMapper; using System; -using DevHive.Common.Models.Identity; using DevHive.Services.Interfaces; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Web.Controllers { - [ApiController] + [ApiController] [Route("/api/[controller]")] //[Authorize(Roles = "Admin")] public class RoleController @@ -23,33 +23,33 @@ namespace DevHive.Web.Controllers } [HttpPost] - public async Task Create([FromBody] CreateRoleModel createRoleModel) + public async Task Create([FromBody] CreateRoleWebModel createRoleWebModel) { - RoleModel roleServiceModel = - this._roleMapper.Map(createRoleModel); + RoleServiceModel roleServiceModel = + this._roleMapper.Map(createRoleWebModel); - bool result = await this._roleService.CreateRole(roleServiceModel); + Guid id = await this._roleService.CreateRole(roleServiceModel); - if (!result) - return new BadRequestObjectResult("Could not create role!"); + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create role {createRoleWebModel.Name}") : + new OkObjectResult(new { Id = id }); - return new OkResult(); } [HttpGet] public async Task GetById(Guid id) { - RoleModel roleServiceModel = await this._roleService.GetRoleById(id); - RoleModel roleWebModel = this._roleMapper.Map(roleServiceModel); + RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); + RoleWebModel roleWebModel = this._roleMapper.Map(roleServiceModel); return new OkObjectResult(roleWebModel); } [HttpPut] - public async Task Update(Guid id, [FromBody] UpdateRoleModel updateRoleModel) + public async Task Update(Guid id, [FromBody] UpdateRoleWebModel updateRoleWebModel) { - RoleModel roleServiceModel = - this._roleMapper.Map(updateRoleModel); + RoleServiceModel roleServiceModel = + this._roleMapper.Map(updateRoleWebModel); roleServiceModel.Id = id; bool result = await this._roleService.UpdateRole(roleServiceModel); diff --git a/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs index becb3c9..e872428 100644 --- a/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs @@ -1,6 +1,6 @@ namespace DevHive.Web.Models.Identity.Role { - public class CreateRoleModel + public class CreateRoleWebModel { public string Name { get; set; } } diff --git a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs index 9e97ffc..41ade23 100644 --- a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs @@ -1,5 +1,3 @@ -using System; - namespace DevHive.Web.Models.Identity.Role { public class RoleWebModel diff --git a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs index 1eaad57..213ec55 100644 --- a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs @@ -1,6 +1,6 @@ namespace DevHive.Web.Models.Identity.Role { - public class UpdateRoleModel : CreateRoleModel + public class UpdateRoleWebModel : CreateRoleWebModel { } } -- cgit v1.2.3 From 163827d4a8dbc4b41bfa0f78edc7dd30626e95d3 Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 19 Jan 2021 19:02:37 +0200 Subject: Fixed role id creeping up the chain --- src/DevHive.Services/Interfaces/IRoleService.cs | 2 +- src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs | 3 --- .../Models/Identity/Role/UpdateRoleServiceModel.cs | 3 +++ src/DevHive.Services/Services/RoleService.cs | 8 ++++---- 4 files changed, 8 insertions(+), 8 deletions(-) (limited to 'src/DevHive.Services/Models/Identity/Role') diff --git a/src/DevHive.Services/Interfaces/IRoleService.cs b/src/DevHive.Services/Interfaces/IRoleService.cs index a7a0e47..fd661be 100644 --- a/src/DevHive.Services/Interfaces/IRoleService.cs +++ b/src/DevHive.Services/Interfaces/IRoleService.cs @@ -10,7 +10,7 @@ namespace DevHive.Services.Interfaces Task GetRoleById(Guid id); - Task UpdateRole(RoleServiceModel roleServiceModel); + Task UpdateRole(UpdateRoleServiceModel roleServiceModel); Task DeleteRole(Guid id); } diff --git a/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs index 3f834ef..07249fe 100644 --- a/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs @@ -1,10 +1,7 @@ -using System; - namespace DevHive.Services.Models.Identity.Role { public class RoleServiceModel { - public Guid Id { get; set; } public string Name { get; set; } } } diff --git a/src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs index be71771..e21e6b4 100644 --- a/src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs @@ -1,7 +1,10 @@ +using System; + namespace DevHive.Services.Models.Identity.Role { public class UpdateRoleServiceModel { + public Guid Id { get; set; } public string Name { get; set; } } } diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index 0945624..3ebb7c8 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -47,15 +47,15 @@ namespace DevHive.Services.Services return this._roleMapper.Map(role); } - public async Task UpdateRole(RoleServiceModel roleServiceModel) + public async Task UpdateRole(UpdateRoleServiceModel updateRoleServiceModel) { - if (!await this._roleRepository.DoesRoleExist(roleServiceModel.Id)) + if (!await this._roleRepository.DoesRoleExist(updateRoleServiceModel.Id)) throw new ArgumentException("Role does not exist!"); - if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) + if (await this._roleRepository.DoesNameExist(updateRoleServiceModel.Name)) throw new ArgumentException("Role name already exists!"); - Role role = this._roleMapper.Map(roleServiceModel); + Role role = this._roleMapper.Map(updateRoleServiceModel); return await this._roleRepository.EditAsync(role); } -- cgit v1.2.3 From 9e86699c9b3aff17e0c4d19850b41b792a9625ef Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 21 Jan 2021 19:12:04 +0200 Subject: Removed HTTP Patch; Refactored HTTP Put; Fixed Update bug --- .../Repositories/LanguageRepository.cs | 7 +- .../Repositories/TechnologyRepository.cs | 1 + src/DevHive.Data/Repositories/UserRepository.cs | 15 ++- .../Configurations/Mapping/RoleMapings.cs | 2 +- .../Configurations/Mapping/TechnologyMappings.cs | 2 + .../Mapping/UserCollectionMappings.cs | 22 ---- src/DevHive.Services/Interfaces/IUserService.cs | 1 - .../Models/Identity/Role/CreateRoleServiceModel.cs | 14 +++ .../Models/Identity/User/FriendServiceModel.cs | 3 + .../Identity/User/UpdateFriendServiceModel.cs | 10 ++ .../User/UpdateUserCollectionServiceModel.cs | 7 -- .../Models/Identity/User/UpdateUserServiceModel.cs | 11 +- src/DevHive.Services/Services/UserService.cs | 135 +++++++++------------ .../Configurations/Mapping/LanguageMappings.cs | 7 +- .../Configurations/Mapping/RoleMappings.cs | 9 +- .../Configurations/Mapping/TechnologyMappings.cs | 3 +- .../Configurations/Mapping/UserMappings.cs | 13 +- src/DevHive.Web/Controllers/UserController.cs | 14 --- .../Models/Identity/User/UpdateUserWebModel.cs | 5 + 19 files changed, 132 insertions(+), 149 deletions(-) delete mode 100644 src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs create mode 100644 src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs create mode 100644 src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs (limited to 'src/DevHive.Services/Models/Identity/Role') diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 108b307..4c51cf3 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -20,8 +20,7 @@ namespace DevHive.Data.Repositories public async Task AddAsync(Language entity) { - await this._context - .Set() + await this._context.Languages .AddAsync(entity); return await this.SaveChangesAsync(this._context); @@ -32,14 +31,14 @@ namespace DevHive.Data.Repositories public async Task GetByIdAsync(Guid id) { - return await this._context - .Set() + return await this._context.Languages .FindAsync(id); } public async Task GetByNameAsync(string languageName) { return await this._context.Languages + .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == languageName); } #endregion diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 390ad3f..a41d4fb 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -40,6 +40,7 @@ namespace DevHive.Data.Repositories public async Task GetByNameAsync(string technologyName) { return await this._context.Technologies + .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == technologyName); } #endregion diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 3f9af70..c769f7e 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -6,6 +6,7 @@ using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; namespace DevHive.Data.Repositories { @@ -78,7 +79,9 @@ namespace DevHive.Data.Repositories public async Task GetByUsernameAsync(string username) { return await this._context.Users - .Include(u => u.Roles) + .AsNoTracking() + .Include(x => x.Languages) + .Include(x => x.Technologies) .FirstOrDefaultAsync(x => x.UserName == username); } @@ -107,9 +110,13 @@ namespace DevHive.Data.Repositories #region Update - public async Task EditAsync(User newEntity) + public async Task EditAsync(User entity) { - this._context.Update(newEntity); + User user = await this._context.Users + .FirstOrDefaultAsync(x => x.Id == entity.Id); + + this._context.Update(user); + this._context.Entry(entity).CurrentValues.SetValues(entity); return await this.SaveChangesAsync(this._context); } @@ -177,6 +184,7 @@ namespace DevHive.Data.Repositories public async Task DoesUserExistAsync(Guid id) { return await this._context.Users + .AsNoTracking() .AnyAsync(x => x.Id == id); } @@ -208,6 +216,7 @@ namespace DevHive.Data.Repositories public bool DoesUserHaveThisUsername(Guid id, string username) { return this._context.Users + .AsNoTracking() .Any(x => x.Id == id && x.UserName == username); } diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index b5541f9..d6c8511 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -12,7 +12,7 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs index 079ec3e..0103ccf 100644 --- a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs @@ -11,8 +11,10 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); CreateMap(); + CreateMap(); CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs deleted file mode 100644 index 7a773e8..0000000 --- a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs +++ /dev/null @@ -1,22 +0,0 @@ -using AutoMapper; -using DevHive.Data.Models; -using DevHive.Services.Models.Identity.User; - -namespace DevHive.Services.Configurations.Mapping -{ - public class UserCollectionMappings : Profile - { - public UserCollectionMappings() - { - CreateMap(); - CreateMap(); - CreateMap(); - CreateMap(); - - CreateMap(); - CreateMap(); - CreateMap(); - CreateMap(); - } - } -} diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index 88be0c8..923e9bb 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -18,7 +18,6 @@ namespace DevHive.Services.Interfaces Task GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); - Task PatchUser(Guid id, List patch); Task DeleteUser(Guid id); Task RemoveFriend(Guid userId, Guid friendId); diff --git a/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs new file mode 100644 index 0000000..53bea9e --- /dev/null +++ b/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs @@ -0,0 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + +namespace DevHive.Services.Models.Identity.Role +{ + public class CreateRoleServiceModel + { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs b/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs index 63d57f7..a784f5c 100644 --- a/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs @@ -1,7 +1,10 @@ +using System; + namespace DevHive.Services.Models.Identity.User { public class FriendServiceModel { + public Guid Id { get; set; } public string UserName { get; set; } } } diff --git a/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs new file mode 100644 index 0000000..83fcc34 --- /dev/null +++ b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Services.Models.Identity.User +{ + public class UpdateFriendServiceModel + { + public Guid Id { get; set; } + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs deleted file mode 100644 index c40a980..0000000 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace DevHive.Services.Models.Identity.User -{ - public class UpdateUserCollectionServiceModel - { - public string Name { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs index 835bf54..9277e3e 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs @@ -1,5 +1,8 @@ using System; using System.Collections.Generic; +using DevHive.Services.Models.Identity.Role; +using DevHive.Services.Models.Language; +using DevHive.Services.Models.Technology; namespace DevHive.Services.Models.Identity.User { @@ -9,13 +12,13 @@ namespace DevHive.Services.Models.Identity.User public string Password { get; set; } - public HashSet Roles { get; set; } = new HashSet(); + public HashSet Roles { get; set; } = new HashSet(); - public HashSet Friends { get; set; } = new HashSet(); + public HashSet Friends { get; set; } = new HashSet(); - public HashSet Languages { get; set; } = new HashSet(); + public HashSet Languages { get; set; } = new HashSet(); - public HashSet Technologies { get; set; } = new HashSet(); + public HashSet Technologies { get; set; } = new HashSet(); } } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index dca00fa..a57fd23 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -14,8 +14,9 @@ using DevHive.Services.Interfaces; using DevHive.Data.Interfaces.Repositories; using System.Linq; using DevHive.Common.Models.Misc; -using System.Reflection; -using Microsoft.CodeAnalysis.CSharp.Syntax; +using DevHive.Services.Models.Language; +using DevHive.Services.Models.Technology; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Services.Services { @@ -135,69 +136,57 @@ namespace DevHive.Services.Services public async Task UpdateUser(UpdateUserServiceModel updateUserServiceModel) { - //Method: ValidateUserOnUpdate - if (!await this._userRepository.DoesUserExistAsync(updateUserServiceModel.Id)) - throw new ArgumentException("User does not exist!"); - - if (!this._userRepository.DoesUserHaveThisUsername(updateUserServiceModel.Id, updateUserServiceModel.UserName) - && await this._userRepository.DoesUsernameExistAsync(updateUserServiceModel.UserName)) - throw new ArgumentException("Username already exists!"); + await this.ValidateUserOnUpdate(updateUserServiceModel); await this.ValidateUserCollections(updateUserServiceModel); - //Method: Insert collections to user - HashSet languages = new(); - foreach (UpdateUserCollectionServiceModel lang in updateUserServiceModel.Languages) - languages.Add(await this._languageRepository.GetByNameAsync(lang.Name) ?? - throw new ArgumentException("Invalid language name!")); + //Preserve roles + int roleCount = updateUserServiceModel.Roles.Count; + for (int i = 0; i < roleCount; i++) + { + Role role = await this._roleRepository.GetByNameAsync(updateUserServiceModel.Roles.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid role name!"); - HashSet technologies = new(); - foreach (UpdateUserCollectionServiceModel tech in updateUserServiceModel.Technologies) - technologies.Add(await this._technologyRepository.GetByNameAsync(tech.Name) ?? - throw new ArgumentException("Invalid technology name!")); + UpdateRoleServiceModel updateRoleServiceModel = this._userMapper.Map(role); - User user = this._userMapper.Map(updateUserServiceModel); + updateUserServiceModel.Roles.Add(updateRoleServiceModel); + } - user.Languages = languages; - user.Technologies = technologies; + int langCount = updateUserServiceModel.Languages.Count; + for (int i = 0; i < langCount; i++) + { + Language language = await this._languageRepository.GetByNameAsync(updateUserServiceModel.Languages.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid language name!"); - bool successful = await this._userRepository.EditAsync(user); + UpdateLanguageServiceModel updateLanguageServiceModel = this._userMapper.Map(language); - if (!successful) - throw new InvalidOperationException("Unable to edit user!"); + updateUserServiceModel.Languages.Add(updateLanguageServiceModel); + } - return this._userMapper.Map(user); ; - } + //Clean the already replaced languages + updateUserServiceModel.Languages.RemoveWhere(x => x.Id == Guid.Empty); - public async Task PatchUser(Guid id, List patchList) - { - User user = await this._userRepository.GetByIdAsync(id) ?? - throw new ArgumentException("User does not exist!"); + int techCount = updateUserServiceModel.Technologies.Count; + for (int i = 0; i < techCount; i++) + { + Technology technology = await this._technologyRepository.GetByNameAsync(updateUserServiceModel.Technologies.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid technology name!"); - UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map(user); + UpdateTechnologyServiceModel updateTechnologyServiceModel = this._userMapper.Map(technology); - foreach (Patch patch in patchList) - { - bool successful = patch.Action switch - { - "replace" => ReplacePatch(updateUserServiceModel, patch), - "add" => AddPatch(updateUserServiceModel, patch), - "remove" => RemovePatch(updateUserServiceModel, patch), - _ => throw new ArgumentException("Invalid patch operation!"), - }; - - if (!successful) - throw new ArgumentException("A problem occurred while applying patch"); + updateUserServiceModel.Technologies.Add(updateTechnologyServiceModel); } - bool success = await this._userRepository.EditAsync(user); - if (success) - { - user = await this._userRepository.GetByIdAsync(id); - return this._userMapper.Map(user); - } - else - return null; + //Clean the already replaced technologies + updateUserServiceModel.Technologies.RemoveWhere(x => x.Id == Guid.Empty); + + User user = this._userMapper.Map(updateUserServiceModel); + bool successful = await this._userRepository.EditAsync(user); + + if (!successful) + throw new InvalidOperationException("Unable to edit user!"); + + return this._userMapper.Map(user); } #endregion @@ -282,10 +271,20 @@ namespace DevHive.Services.Services return toReturn; } + private async Task ValidateUserOnUpdate(UpdateUserServiceModel updateUserServiceModel) + { + if (!await this._userRepository.DoesUserExistAsync(updateUserServiceModel.Id)) + throw new ArgumentException("User does not exist!"); + + if (!this._userRepository.DoesUserHaveThisUsername(updateUserServiceModel.Id, updateUserServiceModel.UserName) + && await this._userRepository.DoesUsernameExistAsync(updateUserServiceModel.UserName)) + throw new ArgumentException("Username already exists!"); + } + private async Task ValidateUserCollections(UpdateUserServiceModel updateUserServiceModel) { // Friends - foreach (UpdateUserCollectionServiceModel friend in updateUserServiceModel.Friends) + foreach (var friend in updateUserServiceModel.Friends) { User returnedFriend = await this._userRepository.GetByUsernameAsync(friend.Name); @@ -294,29 +293,24 @@ namespace DevHive.Services.Services } // Languages - foreach (UpdateUserCollectionServiceModel language in updateUserServiceModel.Languages) + foreach (var language in updateUserServiceModel.Languages) { Language returnedLanguage = await this._languageRepository.GetByNameAsync(language.Name); - if (default(Language) == returnedLanguage) + if (returnedLanguage == null) throw new ArgumentException($"Language {language.Name} does not exist!"); } // Technology - foreach (UpdateUserCollectionServiceModel technology in updateUserServiceModel.Technologies) + foreach (var technology in updateUserServiceModel.Technologies) { Technology returnedTechnology = await this._technologyRepository.GetByNameAsync(technology.Name); - if (default(Technology) == returnedTechnology) + if (returnedTechnology == null) throw new ArgumentException($"Technology {technology.Name} does not exist!"); } } - private async Task ValidateUserOnUpdate(UpdateUserServiceModel updateUserServiceModel) - { - throw new NotImplementedException(); - } - private string WriteJWTSecurityToken(Guid userId, HashSet roles) { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); @@ -344,25 +338,6 @@ namespace DevHive.Services.Services SecurityToken token = tokenHandler.CreateToken(tokenDescriptor); return tokenHandler.WriteToken(token); } - - private bool AddPatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) - { - // Type type = typeof(UpdateUserServiceModel); - // PropertyInfo property = type.GetProperty(patch.Name); - - // property.SetValue(updateUserServiceModel, patch.Value); - throw new NotImplementedException(); - } - - private bool RemovePatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) - { - throw new NotImplementedException(); - } - - private bool ReplacePatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) - { - throw new NotImplementedException(); - } #endregion } } diff --git a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs index 8cac3ca..eca0d1a 100644 --- a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -8,10 +8,11 @@ namespace DevHive.Web.Configurations.Mapping { public LanguageMappings() { - CreateMap(); - CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap(); CreateMap(); CreateMap(); diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 66ae8e3..2ea2742 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -8,11 +8,14 @@ namespace DevHive.Web.Configurations.Mapping { public RoleMappings() { - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap(); + CreateMap(); + CreateMap(); CreateMap(); - CreateMap(); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs index 4ecd5f3..708b6ac 100644 --- a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -10,7 +10,8 @@ namespace DevHive.Web.Configurations.Mapping { CreateMap(); CreateMap(); - CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); CreateMap(); CreateMap(); diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 5faf4b5..9dbf613 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -20,13 +20,14 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); - CreateMap(); - CreateMap(); + //Update + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); - CreateMap() - .ForMember(f => f.Name, u => u.MapFrom(src => src.UserName)); - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 7121ac8..fbbbbff 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -96,20 +96,6 @@ namespace DevHive.Web.Controllers return new AcceptedResult("UpdateUser", userWebModel); } - - [HttpPatch] - public async Task Patch(Guid id, [FromBody] List patch, [FromHeader] string authorization) - { - if (!await this._userService.ValidJWT(id, authorization)) - return new UnauthorizedResult(); - - UserServiceModel userServiceModel = await this._userService.PatchUser(id, patch); - - if (userServiceModel == null) - return new BadRequestObjectResult("Wrong patch properties"); - else - return new OkObjectResult(this._userMapper.Map(userServiceModel)); - } #endregion #region Delete diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 3c38ab6..30c66fb 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; using DevHive.Web.Attributes; +using DevHive.Web.Models.Identity.Role; using DevHive.Web.Models.Language; using DevHive.Web.Models.Technology; @@ -18,6 +19,10 @@ namespace DevHive.Web.Models.Identity.User [Required] public HashSet Friends { get; set; } + [NotNull] + [Required] + public HashSet Roles { get; set; } + [NotNull] [Required] public HashSet Languages { get; set; } -- cgit v1.2.3 From 1f8d51c5393ea2f413de4f3e05f77d360d096ff3 Mon Sep 17 00:00:00 2001 From: transtrike Date: Sat, 23 Jan 2021 16:01:05 +0200 Subject: The beginning of role based authorization --- src/DevHive.Services/Configurations/Mapping/RoleMapings.cs | 1 + src/DevHive.Services/Interfaces/IRoleService.cs | 2 +- .../Models/Identity/Role/CreateRoleServiceModel.cs | 4 ---- src/DevHive.Services/Services/RoleService.cs | 3 +-- src/DevHive.Services/Services/UserService.cs | 2 ++ .../Configurations/Extensions/ConfigureDatabase.cs | 12 ++++++++++++ src/DevHive.Web/Controllers/LanguageController.cs | 3 +++ src/DevHive.Web/Controllers/PostController.cs | 2 +- src/DevHive.Web/Controllers/RoleController.cs | 11 +++++++---- src/DevHive.Web/Controllers/TechnologyController.cs | 3 +++ src/DevHive.Web/Controllers/UserController.cs | 7 +------ 11 files changed, 32 insertions(+), 18 deletions(-) (limited to 'src/DevHive.Services/Models/Identity/Role') diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index d6c8511..5f9452f 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -8,6 +8,7 @@ namespace DevHive.Services.Configurations.Mapping { public RoleMappings() { + CreateMap(); CreateMap(); CreateMap(); diff --git a/src/DevHive.Services/Interfaces/IRoleService.cs b/src/DevHive.Services/Interfaces/IRoleService.cs index fd661be..3a498d2 100644 --- a/src/DevHive.Services/Interfaces/IRoleService.cs +++ b/src/DevHive.Services/Interfaces/IRoleService.cs @@ -6,7 +6,7 @@ namespace DevHive.Services.Interfaces { public interface IRoleService { - Task CreateRole(RoleServiceModel roleServiceModel); + Task CreateRole(CreateRoleServiceModel roleServiceModel); Task GetRoleById(Guid id); diff --git a/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs index 53bea9e..3bed3fd 100644 --- a/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs @@ -5,10 +5,6 @@ namespace DevHive.Services.Models.Identity.Role { public class CreateRoleServiceModel { - [NotNull] - [Required] - [MinLength(3)] - [MaxLength(50)] public string Name { get; set; } } } diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index 896946d..91a8c73 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -20,12 +20,11 @@ namespace DevHive.Services.Services this._roleMapper = mapper; } - public async Task CreateRole(RoleServiceModel roleServiceModel) + public async Task CreateRole(CreateRoleServiceModel roleServiceModel) { if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) throw new ArgumentException("Role already exists!"); - Role role = this._roleMapper.Map(roleServiceModel); bool success = await this._roleRepository.AddAsync(role); diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 533f422..cf33644 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -194,6 +194,8 @@ namespace DevHive.Services.Services return false; /* Check roles */ + if(jwtRoleNames.Contains(Role.AdminRole)) + return true; // Check if jwt contains all user roles (if it doesn't, jwt is either old or tampered with) foreach (var role in user.Roles) diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs index 4831435..6e92a65 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs @@ -8,6 +8,8 @@ using Microsoft.AspNetCore.Builder; using System; using Microsoft.AspNetCore.Authentication.JwtBearer; using DevHive.Data; +using Microsoft.AspNetCore.Authorization; +using System.Collections.Generic; namespace DevHive.Web.Configurations.Extensions { @@ -19,6 +21,7 @@ namespace DevHive.Web.Configurations.Extensions options.UseNpgsql(configuration.GetConnectionString("DEV"))); services.AddIdentity() + .AddRoles() .AddEntityFrameworkStores(); services.Configure(options => @@ -47,6 +50,15 @@ namespace DevHive.Web.Configurations.Extensions options.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme); options.RequireRole("User"); }); + + options.AddPolicy("Administrator", options => + { + options.RequireAuthenticatedUser(); + options.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme); + options.RequireRole("Admin"); + }); + + // options.DefaultPolicy = ; }); } diff --git a/src/DevHive.Web/Controllers/LanguageController.cs b/src/DevHive.Web/Controllers/LanguageController.cs index e2d0dec..c0c5fd1 100644 --- a/src/DevHive.Web/Controllers/LanguageController.cs +++ b/src/DevHive.Web/Controllers/LanguageController.cs @@ -4,12 +4,14 @@ using AutoMapper; using DevHive.Services.Interfaces; using DevHive.Services.Models.Language; using DevHive.Web.Models.Language; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace DevHive.Web.Controllers { [ApiController] [Route("/api/[controller]")] + [Authorize(Policy = "Administrator")] public class LanguageController { private readonly ILanguageService _languageService; @@ -34,6 +36,7 @@ namespace DevHive.Web.Controllers } [HttpGet] + [Authorize(Policy = "User")] public async Task GetById(Guid id) { ReadLanguageServiceModel languageServiceModel = await this._languageService.GetLanguageById(id); diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs index 50923d2..8b7344b 100644 --- a/src/DevHive.Web/Controllers/PostController.cs +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -13,7 +13,6 @@ namespace DevHive.Web.Controllers { [ApiController] [Route("/api/[controller]")] - [Authorize(Roles = "User")] public class PostController { private readonly IPostService _postService; @@ -27,6 +26,7 @@ namespace DevHive.Web.Controllers //Create [HttpPost] + [Authorize(Roles = "User")] public async Task Create([FromBody] CreatePostWebModel createPostModel) { CreatePostServiceModel postServiceModel = diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index 227b877..0206542 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -5,12 +5,12 @@ using AutoMapper; using System; using DevHive.Services.Interfaces; using DevHive.Services.Models.Identity.Role; +using Microsoft.AspNetCore.Authorization; namespace DevHive.Web.Controllers { [ApiController] [Route("/api/[controller]")] - //[Authorize(Roles = "Admin")] public class RoleController { private readonly IRoleService _roleService; @@ -23,20 +23,21 @@ namespace DevHive.Web.Controllers } [HttpPost] + [Authorize(Policy = "Administrator")] public async Task Create([FromBody] CreateRoleWebModel createRoleWebModel) { - RoleServiceModel roleServiceModel = - this._roleMapper.Map(createRoleWebModel); + CreateRoleServiceModel roleServiceModel = + this._roleMapper.Map(createRoleWebModel); Guid id = await this._roleService.CreateRole(roleServiceModel); return id == Guid.Empty ? new BadRequestObjectResult($"Could not create role {createRoleWebModel.Name}") : new OkObjectResult(new { Id = id }); - } [HttpGet] + [Authorize(Policy = "User")] public async Task GetById(Guid id) { RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); @@ -46,6 +47,7 @@ namespace DevHive.Web.Controllers } [HttpPut] + [Authorize(Policy = "Administrator")] public async Task Update(Guid id, [FromBody] UpdateRoleWebModel updateRoleWebModel) { UpdateRoleServiceModel updateRoleServiceModel = @@ -61,6 +63,7 @@ namespace DevHive.Web.Controllers } [HttpDelete] + [Authorize(Policy = "Administrator")] public async Task Delete(Guid id) { bool result = await this._roleService.DeleteRole(id); diff --git a/src/DevHive.Web/Controllers/TechnologyController.cs b/src/DevHive.Web/Controllers/TechnologyController.cs index ba2ffdc..9c6c094 100644 --- a/src/DevHive.Web/Controllers/TechnologyController.cs +++ b/src/DevHive.Web/Controllers/TechnologyController.cs @@ -4,12 +4,14 @@ using AutoMapper; using DevHive.Services.Interfaces; using DevHive.Services.Models.Technology; using DevHive.Web.Models.Technology; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace DevHive.Web.Controllers { [ApiController] [Route("/api/[controller]")] + [Authorize(Policy = "Administrator")] public class TechnologyController { private readonly ITechnologyService _technologyService; @@ -34,6 +36,7 @@ namespace DevHive.Web.Controllers } [HttpGet] + [Authorize(Policy = "User")] public async Task GetById(Guid id) { CreateTechnologyServiceModel createTechnologyServiceModel = await this._technologyService.GetTechnologyById(id); diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index fbbbbff..dd94089 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -7,15 +7,12 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using DevHive.Common.Models.Identity; using DevHive.Services.Interfaces; -using Microsoft.AspNetCore.JsonPatch; -using DevHive.Common.Models.Misc; -using System.Collections.Generic; namespace DevHive.Web.Controllers { [ApiController] [Route("/api/[controller]")] - [Authorize(Roles = "User")] + [Authorize(Policy = "User")] public class UserController : ControllerBase { private readonly IUserService _userService; @@ -56,7 +53,6 @@ namespace DevHive.Web.Controllers #endregion #region Read - [HttpGet] public async Task GetById(Guid id, [FromHeader] string authorization) { @@ -71,7 +67,6 @@ namespace DevHive.Web.Controllers [HttpGet] [Route("GetUser")] - [AllowAnonymous] public async Task GetUser(string username) { UserServiceModel friendServiceModel = await this._userService.GetUserByUsername(username); -- cgit v1.2.3