public sealed override V Process(IExecutionContext executionCtx, ISessionContext sessionCtx, T viewModel) { V response = default(V); if (sessionCtx == null) { if (Authenticate(viewModel)) { UserContext userContext = GetUserContext(viewModel); this.SecurityToken = GenerateToken(userContext.SecurityToken); sessionCtx = SaveUserContext(userContext); UserRoleContext userRoleContext = GetUserRoleContext(); SaveUserRoleContext(userRoleContext, sessionCtx); } } else { if (StartSession(viewModel)) { UserRoleContext userRoleContext = GetUserRoleContext(); SaveUserRoleContext(userRoleContext, sessionCtx); } } response = GetResponse(); return(response); }
/// <summary> /// 判断用户是否包含当前角色。 /// </summary> /// <param name="user">用户实例。</param> /// <param name="normalizedRoleName">验证角色名称。</param> /// <param name="cancellationToken">取消标志。</param> /// <returns>返回判断结果。</returns> public override async Task <bool> IsInRoleAsync(TUser user, string normalizedRoleName, CancellationToken cancellationToken = default) { var role = await RoleManager.FindByNameAsync(normalizedRoleName); if (role == null) { return(false); } return(await UserRoleContext.AnyAsync(x => x.UserId == user.UserId && x.RoleId == role.RoleId, cancellationToken)); }
/// <summary> /// 移除用户角色。 /// </summary> /// <param name="user">用户实例对象。</param> /// <param name="normalizedRoleName">验证角色名称。</param> /// <param name="cancellationToken">取消标志。</param> public override async Task RemoveFromRoleAsync(TUser user, string normalizedRoleName, CancellationToken cancellationToken = default) { var role = await FindRoleAsync(normalizedRoleName, cancellationToken); if (role != null) { await UserRoleContext.BeginTransactionAsync(async db => { if (await db.DeleteAsync(x => x.UserId == user.Id && x.RoleId == role.Id, cancellationToken)) { return(await SetMaxRoleAsync(db.As <TRole>(), user.Id, cancellationToken)); } return(false); }, cancellationToken : cancellationToken); } }
/// <summary> /// 设置用户角色。 /// </summary> /// <param name="userId">用户Id。</param> /// <param name="roleIds">角色Id列表。</param> /// <param name="cancellationToken">取消标识。</param> /// <returns>返回设置结果。</returns> public virtual Task <bool> SetUserToRolesAsync(int userId, int[] roleIds, CancellationToken cancellationToken = default) { return(UserRoleContext.BeginTransactionAsync(async db => { await db.DeleteAsync(x => x.UserId == userId, cancellationToken); foreach (var roleId in roleIds) { if (!await db.CreateAsync(new TUserRole { RoleId = roleId, UserId = userId }, cancellationToken)) { return false; } } return await SetMaxRoleAsync(db.As <TRole>(), userId, cancellationToken); }, cancellationToken: cancellationToken)); }
/// <summary> /// 设置用户角色。 /// </summary> /// <param name="userId">用户Id。</param> /// <param name="roleIds">角色Id列表。</param> /// <returns>返回添加结果。</returns> public virtual bool SetUserToRoles(int userId, int[] roleIds) { return(UserRoleContext.BeginTransaction(db => { db.Delete(x => x.UserId == userId); foreach (var roleId in roleIds) { if (!db.Create(new TUserRole { RoleId = roleId, UserId = userId })) { return false; } } return SetMaxRole(db.As <TRole>(), userId); })); }
/// <summary> /// 添加用户角色。 /// </summary> /// <param name="user">当前用户实例。</param> /// <param name="normalizedRoleName">验证角色名称。</param> /// <param name="cancellationToken">取消标志。</param> public override async Task AddToRoleAsync(TUser user, string normalizedRoleName, CancellationToken cancellationToken = default) { var role = await RoleManager.FindByNameAsync(normalizedRoleName); if (role == null || await UserRoleContext.AnyAsync(x => x.UserId == user.UserId && x.RoleId == role.RoleId, cancellationToken)) { return; } //更新用户表显示角色Id和角色名称 await UserRoleContext.BeginTransactionAsync(async db => { if (!await db.CreateAsync(CreateUserRole(user, role), cancellationToken)) { return(false); } return(await SetMaxRoleAsync(db.As <TRole>(), user.UserId, cancellationToken)); }, cancellationToken : cancellationToken); }
/// <summary> /// Save the logged in user object /// </summary> /// <param name="userRoleContext"></param> /// <param name="sessionCtx"></param> private void SaveUserRoleContext(UserRoleContext userRoleContext, ISessionContext sessionCtx) { SessionContext sessionContext = SessionStore.Get <SessionContext>("SessionContext"); if (sessionContext != null) { sessionContext.UserSecurity = userRoleContext.UserSecurity; sessionContext.UserTransactionLimit = userRoleContext.UserTransactionLimit; sessionContext.UserTask = new List <IUserTask>(); if (userRoleContext.UserSecurity != null && userRoleContext.UserSecurity.Count > 0) { foreach (UserTask userTask in userRoleContext.UserSecurity) { sessionContext.UserTask.Add(userTask); } } } else { throw new FrameworkException(1, "Invalid Session"); } }
/// <summary> /// 获取用户角色实例对象。 /// </summary> /// <param name="userId">用户Id。</param> /// <param name="roleId">角色Id。</param> /// <param name="cancellationToken">取消标识。</param> /// <returns>返回用户角色实例。</returns> protected override Task <TUserRole> FindUserRoleAsync(int userId, int roleId, CancellationToken cancellationToken) { return(UserRoleContext.FindAsync(x => x.UserId == userId && x.RoleId == roleId, cancellationToken)); }
public static IEnumerable <User> GetUsers(string role) { UserRoleContext context = new UserRoleContext(); return(context.Database.SqlQuery <User>("Select * From UserByRole(@role)", new SqlParameter("role", role))); }
public UserRoleRepository() { context = new UserRoleContext(); }