示例#1
0
        /// <summary>
        /// Validation for the User Role Mapping
        /// </summary>
        public static void ValidateAssignUnAssignUsersToRole(UsersToRoleRequest request)
        {
            var dbRole = dbContext.Roles.FirstOrDefault(item => item.Id == request.RoleId);

            if (dbRole == null)
            {
                var response = HttpUtilities.FrameHTTPResp(System.Net.HttpStatusCode.BadRequest, ErrorCodes.ROLE_DOES_NOT_EXISTS);
                throw new HttpResponseException(response);
            }
            //if (request.UserIds == null || request.UserIds.Count()==0)
            //{
            //    var response = HttpUtilities.FrameHTTPResp(System.Net.HttpStatusCode.BadRequest, ErrorCodes.USER_ID_RQD);
            //    throw new HttpResponseException(response);
            //}
            if (dbRole.RoleName.ToUpper() == "ENGINEER")
            {
                foreach (var item in request.UserIds)
                {
                    //  dbContext.Users.Where(item => request.UserIds.Contains(item.Id));
                    var dbUser = dbContext.Users.FirstOrDefault(i => i.Id == item);
                    if (dbUser.EUSR == null)
                    {
                        var response = HttpUtilities.FrameHTTPResp(System.Net.HttpStatusCode.BadRequest, ErrorCodes.EUSR_DOES_NOT_EXISTS);
                        throw new HttpResponseException(response);
                    }
                }
            }
        }
示例#2
0
 public IHttpActionResult AssignUnAssignUsersToRole(UsersToRoleRequest requestList)
 {
     try
     {
         string currentUserEmail = HttpUtilities.GetUserNameFromToken(this.Request);
         ValidationUtilities.ValidateAssignUnAssignUsersToRole(requestList);
         //Get Existing users of the role
         List <UserRoleMap> dbexistingUsers = context.UserRoleMaps.Where(i => i.RoleId == requestList.RoleId && i.Deleted == false).ToList();
         foreach (var item in requestList.UserIds)
         {
             if (dbexistingUsers.FirstOrDefault(i => i.UserId == item && i.RoleId == requestList.RoleId) == null)
             {
                 // if does not exists then only add it.
                 context.UserRoleMaps.Add(new UserRoleMap()
                 {
                     Id        = Guid.NewGuid().ToString(),
                     RoleId    = requestList.RoleId,
                     UserId    = item,
                     CreatedBy = currentUserEmail
                 });
             }
         }
         //remove existing mapping if they dont exists in the input
         foreach (var itemUser in dbexistingUsers)
         {
             if (requestList != null && requestList.UserIds != null)
             {
                 if (!requestList.UserIds.Contains(itemUser.UserId))
                 {
                     itemUser.Deleted = true;
                     context.Entry(itemUser).State = System.Data.Entity.EntityState.Modified;
                 }
             }
         }
         context.SaveChanges();
         return(Ok());
     }
     catch (HttpResponseException ex)
     {
         throw ex;
     }
     catch (Exception ex)
     {
         HttpUtilities.ServerError(ex, Request);
         return(null);
     }
 }