public AspNetUsersDTO Authenticate(LoginDTO loginDTO) { _context = (AVOUMContext)DbManager.GetContext(loginDTO.ProductType, loginDTO.ServerType); var user = _context.AspNetUsers.SingleOrDefault(x => x.UserName == loginDTO.Username); // check if username exists if (user == null) { return(null); } byte[] passwordSalt = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; //// check if password is correct if (!Utilities.VerifyPasswordHash(loginDTO.Password, user.PasswordHash, passwordSalt)) { return(null); } // authentication successful AspNetUsersDTO userDTO = _mapper.Map <AspNetUsersDTO>(user); return(userDTO); // return new LoginResponse { Status = BusinessStatus.Created, log = user, ResponseMessage = $"Product code {user.Username} created successfully!! " }; }
public UserLoginResponse GetUserType(string username, string productType, string serverType) { UserLoginType userLoginType = new UserLoginType(); _context = (AVOUMContext)DbManager.GetContext(productType, serverType); // _context = new AVOUMContext(DbManager.GetDbConnectionString(productType)); var user = _context.AspNetUsers.SingleOrDefault(x => x.UserName == username); if (user != null) { userLoginType.IsFirstTimeLogin = user.FirstTimeLogin; userLoginType.Id = user.Id; var loginProvider = _context.AspNetUserTokens.Where(x => x.UserId == user.Id).FirstOrDefault(); if (loginProvider != null) { userLoginType.LoginProvider = loginProvider.LoginProvider; } else { userLoginType.LoginProvider = "Form"; } //userLoginType.Status = BusinessStatus.Ok; return(new UserLoginResponse { Status = BusinessStatus.Ok, userLogin = userLoginType, Id = userLoginType.IsFirstTimeLogin.ToString(), ResponseMessage = $"UserName Exist" }); } else { return(new UserLoginResponse { Status = BusinessStatus.NotFound, ResponseMessage = $"UserName does not Exist" }); } }
/// <summary> /// Gets the mas permissions. /// </summary> /// <param name="perType">Type of the per.</param> /// <param name="apiContext">The API context.</param> /// <returns></returns> public IEnumerable <MasPermissionDTO> GetMasPermissions(string perType, ApiContext apiContext) { _context = (AVOUMContext)DbManager.GetContext(apiContext.ProductType, apiContext.ServerType); IEnumerable <TblMasPermission> _permissions = _context.TblMasPermission.Where(per => per.ItemType == perType); IEnumerable <MasPermissionDTO> _masPermissionDTOs = _mapper.Map <IEnumerable <MasPermissionDTO> >(_permissions); return(_masPermissionDTOs); }
public LoginResponse GenerateToken(AspNetUsersDTO user, string productType, string serverType) { LoginResponse loginResponse = new LoginResponse(); _context = (AVOUMContext)DbManager.GetContext(productType, serverType); var userDetails = _context.TblUserDetails.FirstOrDefault(u => u.UserName == user.UserName); //var roleDetails = from ro in _context.AspNetRoles // join ur in _context.AspNetUserRoles on ro.Id equals ur.RoleId // where ur.UserId == user.Id // select ur; var roleName = _context.AspNetRoles.FirstOrDefault(u => u.Id == userDetails.RoleId).Name; var issuer = _config["Jwt:Issuer"]; var audience = _config["Jwt:Audience"]; var expiry = DateTime.Now.AddMinutes(120); var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"])); var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256); // Add standard claims var claims = new List <Claim> { new Claim("UserId", user.Id), new Claim("Email", user.Email), new Claim("OrgId", Convert.ToString(userDetails.OrganizationId)), new Claim("PartnerId", Convert.ToString(userDetails.PartnerId)), new Claim("Role", roleName), new Claim("Name", userDetails.FirstName), new Claim("UserName", userDetails.UserName), new Claim("ProductType", productType), new Claim("ServerType", serverType), }; var token = new JwtSecurityToken(issuer: issuer, audience: audience, claims: claims, expires: DateTime.Now.AddMinutes(120), signingCredentials: credentials); var tokenHandler = new JwtSecurityTokenHandler(); var stringToken = tokenHandler.WriteToken(token); loginResponse.Token = stringToken; loginResponse.UserId = user.Id; loginResponse.RoleId = userDetails.RoleId; loginResponse.UserName = user.UserName; loginResponse.FirstName = userDetails.FirstName; loginResponse.LastName = userDetails.LastName; loginResponse.IsMale = userDetails.GenderId == 1001 ? true : false; loginResponse.DisplayName = loginResponse.FirstName + " " + loginResponse.LastName; loginResponse.Status = BusinessStatus.Ok; return(loginResponse); }
/// <summary> /// Gets the permissions. /// </summary> /// <param name="perType">Type of the per.</param> /// <param name="userId">The user identifier.</param> /// <param name="roleId">The role identifier.</param> /// <param name="apiContext">The API context.</param> /// <returns></returns> public IEnumerable <MasPermissionDTO> GetPermissions(string perType, string userId, string roleId, ApiContext apiContext) { _context = (AVOUMContext)DbManager.GetContext(apiContext.ProductType, apiContext.ServerType); IEnumerable <TblMasPermission> _permissions = from maspermission in _context.TblMasPermission join permission in (from rolepermission in _context.TblUserPermissions where rolepermission.RoleId == roleId && rolepermission.UserorRole == "Role" select rolepermission.PermissionId) .Except( from userpermission in _context.TblUserPermissions where userpermission.UserId == userId && userpermission.UserorRole == "User" select userpermission.PermissionId ) on maspermission.PermissionId equals permission.Value where maspermission.ItemType == perType orderby maspermission.SortOrderBy ascending select maspermission; IEnumerable <MasPermissionDTO> _masPermissionDTOs = _permissions .Where(c => (c.ParentId == 0)) .Select(c => new MasPermissionDTO() { PermissionId = c.PermissionId, ItemType = c.ItemType, ParentId = c.ParentId, MenuId = c.MenuId, ItemDescription = c.ItemDescription, Label = c.ItemDescription, Url = c.Url, PathTo = c.PathTo, Collapse = c.Collapse, State = c.State, Mini = c.Mini, Icon = c.Icon, Redirect = c.Redirect, Component = c.Component, Children = GetChildren(_permissions, c.PermissionId) }); // return(_masPermissionDTOs); }
/// <summary> /// Saves the assign permission. /// </summary> /// <param name="permissionIds">The permission ids.</param> /// <param name="apiContext">The API context.</param> /// <returns></returns> public UserPermissionResponse SaveAssignPermission(UserRolesPermissionDTO permissionIds, ApiContext apiContext) { _context = (AVOUMContext)DbManager.GetContext(apiContext.ProductType, apiContext.ServerType); TblUserPermissions userPermissions = null; foreach (var item in permissionIds.RolePermissionIds) { var newPermission = item.PermissionIds.ToList(); var existingPerm = _context.TblUserPermissions.Where(t => t.UserId == permissionIds.UserId && t.UserorRole == "User" && t.RoleId == item.RoleId).ToList(); //Delete which are not in current permissions-- var delPermission = existingPerm.Where(m => !item.PermissionIds.Contains((int)m.PermissionId)).ToList(); foreach (var perm in delPermission) { _context.Remove(perm); existingPerm.Remove(perm); } var includedPermission = existingPerm.Where(m => item.PermissionIds.Contains((int)m.PermissionId)).ToList(); foreach (var incPerm in includedPermission) { newPermission.Remove((int)incPerm.PermissionId); } //Add new record foreach (var permissionId in newPermission) { userPermissions = new TblUserPermissions(); userPermissions.UserId = permissionIds.UserId; userPermissions.PermissionId = permissionId; userPermissions.RoleId = item.RoleId; userPermissions.UserorRole = "User"; // userPermissions.CreatedBy = CreatedBy; userPermissions.CreatedDate = DateTime.Now; userPermissions.Status = true; _context.TblUserPermissions.Add(userPermissions); } } _context.SaveChanges(); return(new UserPermissionResponse { Status = BusinessStatus.Created, Id = userPermissions?.UserPermissionsId.ToString(), ResponseMessage = $"Assigned Permissions successfully!!" }); }
public static DbContext GetContext(string product, string connectionKey) { DbContext context = null; string dbConnectionString = DbConnectionManager.GetConnectionString(connectionKey); switch (product) { case "Mica": var optionsBuilder = new DbContextOptionsBuilder <MICAUMContext>(); optionsBuilder.UseSqlServer(dbConnectionString); //DbContextOptions<MICAUMContext> dbContextOption = (DbContextOptions<MICAUMContext>)SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), dbConnectionString).Options; context = new MICAUMContext(optionsBuilder.Options); break; case "Avo": var avoOptionsBuilder = new DbContextOptionsBuilder <AVOUMContext>(); avoOptionsBuilder.UseSqlServer(dbConnectionString); //DbContextOptions<MICAUMContext> dbContextOption = (DbContextOptions<MICAUMContext>)SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), dbConnectionString).Options; context = new AVOUMContext(avoOptionsBuilder.Options); break; case "Motor": //var motorOptionsBuilder = new DbContextOptionsBuilder<Mot>(); //motorOptionsBuilder.UseSqlServer(dbConnectionString); ////DbContextOptions<MICAUMContext> dbContextOption = (DbContextOptions<MICAUMContext>)SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), dbConnectionString).Options; //context = new AVOUMContext(motorOptionsBuilder.Options); break; default: var optionsBuilderDefault = new DbContextOptionsBuilder <MICAUMContext>(); optionsBuilderDefault.UseSqlServer(dbConnectionString); // DbContextOptions<MICAUMContext> dbDefaultContextOption = (DbContextOptions<MICAUMContext>)SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), dbConnectionString).Options; context = new MICAUMContext(optionsBuilderDefault.Options); break; } return(context); }
/// <summary> /// Assigns the permission. /// </summary> /// <param name="permissionIds">The permission ids.</param> /// <param name="apiContext">The API context.</param> /// <returns></returns> public UserPermissionResponse AssignPermission(UserPermissionDTO permissionIds, ApiContext apiContext) { _context = (AVOUMContext)DbManager.GetContext(apiContext.ProductType, apiContext.ServerType); UserPermissionsDTO userPermissions = null; for (int i = 0; i < permissionIds.PermissionIds.Length; i++) { userPermissions = new UserPermissionsDTO(); userPermissions.UserId = permissionIds.UserId; userPermissions.PermissionId = Convert.ToInt16(permissionIds.PermissionIds[i]); userPermissions.UserorRole = "User"; // userPermissions.CreatedBy = CreatedBy; userPermissions.CreatedDate = DateTime.Now; userPermissions.Status = true; var _usersPer = _mapper.Map <TblUserPermissions>(userPermissions); _context.TblUserPermissions.Add(_usersPer); } _context.SaveChanges(); //return userPermissions; return(new UserPermissionResponse { Status = BusinessStatus.Created, perm = userPermissions, ResponseMessage = $"Assigned {userPermissions.PermissionId} Permissions successfully!!" }); }
/// <summary> /// Gets the user permissions. /// </summary> /// <param name="perType">Type of the per.</param> /// <param name="userId">The user identifier.</param> /// <param name="apiContext">The API context.</param> /// <returns></returns> public IEnumerable <MasPermissionDTO> GetUserPermissions(string perType, string userId, ApiContext apiContext) { _context = (AVOUMContext)DbManager.GetContext(apiContext.ProductType, apiContext.ServerType); var _roles = _context.AspNetUserRoles.Where(userrole => userrole.UserId == userId).Select(role => role.RoleId); IEnumerable <TblMasPermission> _permissions = from maspermission in _context.TblMasPermission join permission in (from rolepermission in _context.TblUserPermissions where _roles.Contains(rolepermission.RoleId) && rolepermission.UserorRole == "Role" select rolepermission.PermissionId) .Except( from userpermission in _context.TblUserPermissions where userpermission.UserId == userId && userpermission.UserorRole == "User" select userpermission.PermissionId ) on maspermission.PermissionId equals permission.Value where maspermission.ItemType == perType select maspermission; IEnumerable <MasPermissionDTO> _masPermissionDTOs = _mapper.Map <IEnumerable <MasPermissionDTO> >(_permissions); return(_masPermissionDTOs); }
/// <summary> /// Gets the user role permissions. /// </summary> /// <param name="userPermissionDTO">The user permission dto.</param> /// <param name="apiContext">The API context.</param> /// <returns></returns> private IEnumerable <MasPermissionDTO> GetUserRolePermissions(UserRoleMapDTO userPermissionDTO, ApiContext apiContext) { _context = (AVOUMContext)DbManager.GetContext(apiContext.ProductType, apiContext.ServerType); var ruleNames = _context.AspNetRoles.Where(r => userPermissionDTO.RoleId.Contains(r.Id)).ToList(); var menuPermission = (from permission in _context.TblUserPermissions join c in _context.TblMasPermission on permission.PermissionId equals c.PermissionId where userPermissionDTO.RoleId.Contains(permission.RoleId) && permission.UserorRole == "Role" select new MasPermissionDTO { PermissionId = c.PermissionId, ItemType = c.ItemType, ParentId = c.ParentId, MenuId = c.MenuId, ItemDescription = c.ItemDescription, Label = c.ItemDescription, Url = c.Url, PathTo = c.PathTo, Collapse = c.Collapse, State = c.State, Mini = c.Mini, Icon = c.Icon, Redirect = c.Redirect, Component = c.Component, Status = true, RoleId = permission.RoleId, RoleName = ruleNames.First(r => r.Id == permission.RoleId).Name }).ToList(); var userPermissions = from c in _context.TblMasPermission join permission in _context.TblUserPermissions on c.PermissionId equals permission.PermissionId where permission.UserId == userPermissionDTO.UserId && permission.UserorRole == "User" select permission; if (userPermissions.Any()) { foreach (var item in userPermissions) { var mPermission = menuPermission.FirstOrDefault(m => m.PermissionId == item.PermissionId && m.RoleId == item.RoleId); if (mPermission != null) { mPermission.Status = false; } } } IEnumerable <MasPermissionDTO> _masPermissionDTOs = menuPermission .Where(c => (c.ParentId == 0)) .Select(c => new MasPermissionDTO() { PermissionId = c.PermissionId, ItemType = c.ItemType, ParentId = c.ParentId, MenuId = c.MenuId, ItemDescription = c.ItemDescription, Label = c.ItemDescription, Url = c.Url, PathTo = c.PathTo, Collapse = c.Collapse, State = c.State, Mini = c.Mini, Icon = c.Icon, Redirect = c.Redirect, Component = c.Component, Status = c.Status, RoleId = c.RoleId, RoleName = c.RoleName, Children = GetMenuChildren(menuPermission, c.PermissionId, c.RoleId) }); return(_masPermissionDTOs); }