Skip to content

Commit

Permalink
#2 Добавил получение данных из таблиц/удаление/назначение прав
Browse files Browse the repository at this point in the history
  • Loading branch information
GregoryGhost committed Mar 12, 2020
1 parent 9960d65 commit 829dfc2
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 17 deletions.
8 changes: 8 additions & 0 deletions Idone/Idone.DAL/Base/Extensions/DbSetExtension.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Idone.DAL.Base.Extensions
{
using Idone.DAL.Dictionaries;

using LanguageExt;

using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -37,5 +39,11 @@ public static Option<T> Find<T>(this DbSet<T> dbSet, params object[] keyValues)
? Some(searchResult)
: None;
}

public static Either<Error, T> FindEither<T>(this DbSet<T> dbSet, params object[] keyValues)
where T : class
{
return dbSet.Find<T>(keyValues).ToEither(Error.NotFoundRecord);
}
}
}
4 changes: 3 additions & 1 deletion Idone/Idone.DAL/DTO/DtoNewPermission.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

public class DtoNewPermission: Record<DtoNewPermission>
{
public DtoNewPermission(string name)
public DtoNewPermission(string name, string description)
{
Name = name;
Description = description;
}

public string Name { get; }
public string Description { get; }
}
}
3 changes: 2 additions & 1 deletion Idone/Idone.DAL/DTO/DtoPermission.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

public class DtoPermission: Record<DtoPermission>, IIdentity
{
public DtoPermission(string name)
public DtoPermission(int id, string name)
{
Name = name;
Id = id;
}

public string Name { get; }
Expand Down
2 changes: 1 addition & 1 deletion Idone/Idone.DAL/DTO/DtoRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class DtoRole : Record<DtoRole>, IIdentity
/// Конструктор по умолчанию.
/// </summary>
/// <param name="name"> Название роли. </param>
public DtoRole(string name)
public DtoRole(int id, string name)
{
Name = name;
}
Expand Down
3 changes: 2 additions & 1 deletion Idone/Idone.DAL/Dictionaries/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public enum Error
/// <summary>
/// Исключение, подробности смотреть в логах.
/// </summary>
Exception = 0
Exception = 0,
NotFoundRecord,
}
}
12 changes: 12 additions & 0 deletions Idone/Idone.DAL/Entities/Permission.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Idone.DAL.Entities
{
using Idone.DAL.Dictionaries;
using Idone.DAL.DTO;

using LanguageExt;

Expand Down Expand Up @@ -41,5 +42,16 @@ private Permission()

return permission;
}

public static Either<Error, Permission> Create(DtoNewPermission newPermission)
{
var user = new Permission
{
Description = newPermission.Description,
Name = newPermission.Name
};

return user;
}
}
}
4 changes: 2 additions & 2 deletions Idone/Idone.DAL/Entities/RolePermission.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ private RolePermission()
/// </summary>
public Role Role { get; private set; }

public Either<Error, RolePermission> Create(Permission permission, Role role)
public static Either<Error, RolePermission> Create(Permission permission, Role role)
{
return new RolePermission
{
Permission = permission,
Role = Role
Role = role
};
}
}
Expand Down
21 changes: 16 additions & 5 deletions Idone/Idone.Security/EnterPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,23 @@ public EnterPoint(IServiceProvider serviceProvider)
/// <inheritdoc />
public Either<Error, Success> AllowRolePermissions(DtoLinkRolePermissions linkRolePermissions)
{
throw new NotImplementedException();
var result = _userService.AllowRolePermissions(linkRolePermissions);

return result;
}

/// <inheritdoc />
public Either<Error, DtoPermission> CreatePermissions(DtoNewPermission newPermission)
{
throw new NotImplementedException();
var result = _userService.CreatePermissions(newPermission);

return result;
}

/// <inheritdoc />
public Either<Error, DAL.DTO.DtoRole> CreateRole(DtoNewRole newRole)
{
//TODO: не стыковка API - в описании модуля создание одной роли, в методе юзер сервиса написано создание ролей, но создает все равно одну роль.
var result = _userService.CreateRoles(newRole);

return result;
Expand All @@ -57,7 +62,9 @@ public EnterPoint(IServiceProvider serviceProvider)
/// <inheritdoc />
public Either<Error, Success> DenyRolePermissions(DtoLinkRolePermissions linkRolePermissions)
{
throw new NotImplementedException();
var result = _userService.DenyRolePermissions(linkRolePermissions);

return result;
}

/// <inheritdoc />
Expand Down Expand Up @@ -136,7 +143,9 @@ public EnterPoint(IServiceProvider serviceProvider)
/// <inheritdoc />
public Either<Error, DtoPermission> GetPermissionById(int permId)
{
throw new NotImplementedException();
var result = _userService.GetPermissionById(permId);

return result;
}

/// <inheritdoc />
Expand Down Expand Up @@ -182,7 +191,9 @@ public EnterPoint(IServiceProvider serviceProvider)
/// <inheritdoc />
public Either<Error, Success> UnsetUserRoles(DtoLinkUserRoles linkUserRoles)
{
throw new NotImplementedException();
var result = _userService.UnsetUserRoles(linkUserRoles);

return result;
}
}
}
91 changes: 85 additions & 6 deletions Idone/Idone.Security/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public UserService(AppContext appContext)
}).Bind(
role =>
{
var createdRole = new DtoRole(role.Name);
var createdRole = new DtoRole(role.Id, role.Name);
return Right<Error, DtoRole>(createdRole);
});

Expand Down Expand Up @@ -168,11 +168,6 @@ public UserService(AppContext appContext)
return Right<Error, DtoGridUser>(result);
}

public Either<Error, DtoRole> GetRoleById(int roleId)
{
throw new System.NotImplementedException();
}

public Either<Error, DtoGridPermission> GetGridRolePermissions(DtoGridQueryRolePermission gridQuery)
{
var dbQuery = _appContext.RolePermissions.AsQueryable();
Expand Down Expand Up @@ -242,5 +237,89 @@ public UserService(AppContext appContext)

return Right<Error, DtoGridPermission>(result);
}

public Either<Error, DtoRole> GetRoleById(int roleId)
{
var dbQuery = _appContext.Roles.AsQueryable().FirstOrDefault(role => role.Id == roleId) ?? Left<Error, Role>(Error.NotFoundRecord);

var result = dbQuery.Bind<DtoRole>(x => new DtoRole(x.Id, x.Name));

return result;
}

public Either<Error, Success> UnsetUserRoles(DtoLinkUserRoles link)
{
var dbQuery = _appContext.UserRoles.AsQueryable();
var foundUser = dbQuery.FirstOrDefault(x => x.Id == link.UserId) ?? Left<Error, UserRole>(Error.NotFoundRecord);

var result = foundUser.Bind(userRole =>
{
_appContext.UserRoles.Remove(userRole);
var t = _appContext.TrySaveChanges().Bind<Success>(_ => Success.ItsSuccess);
return t;
});

return result;
}

public Either<Error, DtoPermission> GetPermissionById(int permId)
{
var dbQuery = _appContext.Permissions.AsQueryable().FirstOrDefault(permission => permission.Id == permId) ?? Left<Error, Permission>(Error.NotFoundRecord);

var result = dbQuery.Bind<DtoPermission>(x => new DtoPermission(x.Id, x.Name));

return result;
}

public Either<Error, Success> DenyRolePermissions(DtoLinkRolePermissions link)
{
var dbQuery = _appContext.RolePermissions.AsQueryable();
var foundUser = dbQuery.FirstOrDefault(x => x.Id == link.RoleId) ?? Left<Error, RolePermission>(Error.NotFoundRecord);

var result = foundUser.Bind(rolePermission =>
{
_appContext.RolePermissions.Remove(rolePermission);
var t = _appContext.TrySaveChanges().Bind<Success>(_ => Success.ItsSuccess);
return t;
});

return result;
}

public Either<Error, DtoPermission> CreatePermissions(DtoNewPermission newPermission)
{
//TODO: опять не состыковочка в названии метода, проверить с тестами, может имеет смысл исправить интерфейс метода.
var result = Permission.Create(newPermission).Bind(
permission =>
{
_appContext.Permissions.Add(permission);
return _appContext.TrySaveChanges().Bind<Permission>(_ => permission);
}).Bind(
permission =>
{
var createdPermission = new DtoPermission(permission.Id, permission.Name);
return Right<Error, DtoPermission>(createdPermission);
});

return result;
}

public Either<Error, Success> AllowRolePermissions(DtoLinkRolePermissions link)
{
var result = _appContext.Roles.FindEither(link.RoleId).Bind(
role =>
{
link.PermissionIds.Select(permission => _appContext.Permissions.Find<Permission>(permission)).Select(
permission =>
{
RolePermission.Create(permission, role).Bind<EntityEntry<RolePermission>>(x => _appContext.RolePermissions.Add(x));
return Right<Error, Permission>(permission);
});
var t = _appContext.TrySaveChanges().Bind<Success>(_ => Success.ItsSuccess);
return t;
});

return result;
}
}
}

0 comments on commit 829dfc2

Please sign in to comment.