private void ValidatePermissionSetViewModel(PermissionSetViewModel permissionViewModel)
 {
     if (permissionViewModel.PermissionSetName.Trim().IsEmpty() || permissionViewModel.PermissionSetName.Trim().IsNull())
         ModelState.AddModelError("PermissionName", "Permission Name is mandatory.");
     if (permissionViewModel.DepartmentId == 0)
         ModelState.AddModelError("DepartmentId", "Please select a Department.");
     if (permissionViewModel.ModuleId == 0)
         ModelState.AddModelError("ModuleId", "Please select a Module.");
     if (!permissionViewModel.PermissionSelected.IsCollectionValid())
         ModelState.AddModelError("PermissionSelected", "Please select a Permission.");
 }
 private void ValidateInputPermissionSet(PermissionSetViewModel permissionViewModel)
 {
     foreach (var permSet in permissionViewModel.PermissionSetViewModels)
     {
         if (permissionViewModel.PermissionSetName.Equals(permSet.PermissionSetName))
             ModelState.AddModelError("PermissionSetName", "Permission Set Name '{0}' already exists.".ToFormat(permissionViewModel.PermissionSetName));
         var permInSet = permissionViewModel.PermissionSetViewModels
             .Where(x => x.DepartmentId == permissionViewModel.DepartmentId && x.ModuleId == permissionViewModel.ModuleId
             && x.IsActive && x.PermissionSetName == permissionViewModel.PermissionSetName).ToList();
         if (permInSet.IsCollectionValid())
             ModelState.AddModelError("", "The permission set name '{0}' with the same department and module already exists.".ToFormat(permissionViewModel.PermissionSetName));
     }
 }
 private void AssignPermissionViewModel(PermissionSetViewModel permissionViewModel)
 {
     var department = _wcfService.InvokeService<IOrganizationService, List<DepartmentInOrganization>>((svc) => svc.GetDepartmentInOrganization(SessionContext.CurrentUser.OrganizationId.Value));
     if (department.IsCollectionValid())
     {
         foreach (var dept in department)
         {
             var departmentInOrg = _wcfService.InvokeService<IOrganizationService, Department>((svc) => svc.GetDepartmentById(dept.DepartmentId.Value));
             permissionViewModel.Department.Add(departmentInOrg);
         }
     }
     var module = _wcfService.InvokeService<IMasterService, List<Module>>((svc) => svc.GetModule());
     var permission = _wcfService.InvokeService<IMasterService, List<Permission>>((svc) => svc.GetPermission());
     if (permission.IsCollectionValid())
         permissionViewModel.Permission = permission;
     var permInSets = _wcfService.InvokeService<IOrganizationService, List<PermissionSet>>((svc) =>
            svc.GetAllPermissionSet(SessionContext.CurrentUser.OrganizationId.Value));
     if (permInSets.IsCollectionValid())
     {
         foreach (var perSet in permInSets)
         {
             permissionViewModel.PermissionSetName = perSet.PermissionSetName;
             permissionViewModel.PermissionSetId = perSet.Id;
             permissionViewModel.PermissionSetDescription = perSet.PermissionSetDescription;
             permissionViewModel.DepartmentName = permissionViewModel.Department.FirstOrDefault(x => x.Id == perSet.DepartmentId.Value).DepartmentName;
             permissionViewModel.ModuleName = module.FirstOrDefault(x => x.Id == perSet.ModuleId.Value).ModuleName;
             List<long> permIds = new List<long>();
             List<string> perNames = new List<string>();
             foreach (var permId in perSet.PermissionInSets)
             {
                 permIds.Add(permId.PermissionId.Value);
             }
             foreach (var id in permIds)
             {
                 var permissionName = permission.FirstOrDefault(x => x.Id == id).PermissionName;
                 perNames.Add(permissionName);
             }
             permissionViewModel.PermissionName = string.Join(",", perNames.ToArray());
             permissionViewModel.IsActive = perSet.Active.Value;
             permissionViewModel.PermissionSetViewModels.Add(permissionViewModel);
         }
     }
 }
 public ActionResult AddPermission(PermissionSetViewModel permissionViewModel, string buttonType)
 {
     if (buttonType == "Next")
         return RedirectToAction("RoleInPermission", "Dashboard");
     ValidatePermissionSetViewModel(permissionViewModel);
     if (ModelState.IsValid)
     {
         permissionViewModel.IsActive = true;
         permissionViewModel.Department = new List<Department>();
         permissionViewModel.Module = new List<Module>();
         permissionViewModel.PermissionSetViewModels = new List<PermissionSetViewModel>();
         AssignPermissionViewModel(permissionViewModel);
         ValidateInputPermissionSet(permissionViewModel);
         if (ModelState.IsValid)
         {
             var mappedPermissionSetModel = Mapper.Map<PermissionSetViewModel, PermissionSet>(permissionViewModel);
             _wcfService.InvokeService<IOrganizationService>((svc) => svc.AddPermission(mappedPermissionSetModel));
             permissionViewModel.CreationSuccessful = true;
             permissionViewModel.Message = "The permission set '{0}' is saved successfully.".ToFormat(permissionViewModel.PermissionSetName);
         }
     }
     return View(permissionViewModel);
 }
 public ActionResult AddPermission()
 {
     var permissionViewModel = new PermissionSetViewModel()
     {
         Department = new List<Department>(),
         Permission = new List<Permission>(),
         Module = new List<Module>(),
         PermissionSetViewModels = new List<PermissionSetViewModel>()
     };
     AssignPermissionViewModel(permissionViewModel);
     return View(permissionViewModel);
 }