public void AddUserToDefaultGroup(string userId, string createdBy) { var defaultGroup = _entities.AspNetGroups.FirstOrDefault(x => x.Code.ToLower() == "staffs" && x.Deleted == false && x.Active == true); if (defaultGroup != null) { //Add a record in table aspNetGroupUsers AspNetGroupUser groupUser = new AspNetGroupUser(); groupUser.UserId = userId; groupUser.GroupId = defaultGroup.Id; try { _groupUsersRepository.Add(groupUser); } catch { return; } try { _applicationLoggingRepository.Log("EVENT", "CREATE", "AspNetGroupUser", userId + '-' + defaultGroup.Id, "", "", groupUser, "", System.Web.HttpContext.Current.Request.UserHostAddress, createdBy); } catch { } try { //Add all Roles of defaultGroup from table aspNetGroupRoles to table aspNetUserRoles IEnumerable <AspNetGroupRole> listRoleOfGroup = _aspNetGroupRolesRepository.GetAll().Where(n => n.GroupId == defaultGroup.Id); if (listRoleOfGroup.Count() > 0) { foreach (var item in listRoleOfGroup) { AspNetUserRole itemUserRole = new AspNetUserRole(); itemUserRole.UserId = userId; itemUserRole.RoleId = item.RoleId; itemUserRole.Grant = true; _aspNetUserRolesRepository.Add(itemUserRole); try { _applicationLoggingRepository.Log("EVENT", "CREATE", "AspNetUserRole", userId + '-' + item.RoleId, "", "", itemUserRole, "", System.Web.HttpContext.Current.Request.UserHostAddress, createdBy); } catch { } } } } catch { } } }
public BaseResponse <AspNetGroupUser> AddGroupForUser(ComplexGroupUser model) { var response = new BaseResponse <AspNetGroupUser>(); AspNetGroupUser groupUser = new AspNetGroupUser(); AspNetUserRole userRole; try { groupUser.UserId = model.UserId; groupUser.GroupId = model.GroupId; response.Value = _groupUsersRepository.Add(groupUser); try { _applicationLoggingRepository.Log("EVENT", "CREATE", "AspNetGroupUser", response.Value.UserId + '-' + response.Value.GroupId, "", "", model, "", System.Web.HttpContext.Current.Request.UserHostAddress, model.CreatedBy); } catch { } IEnumerable <AspNetGroupRole> listRoleOfGroup = _aspNetGroupRolesRepository.GetAll().Where(n => n.GroupId == model.GroupId); if (listRoleOfGroup.Count() > 0) { foreach (var item in listRoleOfGroup) { IEnumerable <AspNetUserRole> listUserRole = _aspNetUserRolesRepository.GetAll().Where(n => n.RoleId == item.RoleId && n.UserId == model.UserId); if (listUserRole.Count() == 0) { userRole = new AspNetUserRole(); userRole.RoleId = item.RoleId; userRole.UserId = model.UserId; userRole.Grant = true; AddUserRole(userRole); try { _applicationLoggingRepository.Log("EVENT", "CREATE", "AspNetUserRole", userRole.UserId + '-' + userRole.RoleId, "", "", model, "", System.Web.HttpContext.Current.Request.UserHostAddress, model.CreatedBy); } catch { } } } } } catch (Exception ex) { response.IsSuccess = false; response.Message = "Error: " + ex.Message + " StackTrace: " + ex.StackTrace; } return(response); }