예제 #1
0
        public ActionResult Clone(string id)
        {
            var userToClone = _userRepository.GetNullableById(id);

            if (userToClone == null)
            {
                ErrorMessage = string.Format("User {0} not found.", id);
                return(this.RedirectToAction(a => a.Index()));
            }

            var newUser = new User {
                Organizations = userToClone.Organizations.ToList(), IsActive = true
            };

            var model = new DepartmentalAdminModel
            {
                User = newUser
            };

            Message =
                string.Format(
                    "Please enter the new user's information. Department associations for {0} have been selected by default.",
                    userToClone.FullNameAndId);

            //Using the modify departmental since it already has the proper logic
            return(View("ModifyDepartmental", model));
        }
예제 #2
0
        public ActionResult ModifyDepartmental(string id)
        {
            var user = _userRepository.Queryable.Where(x => x.Id == id).Fetch(x => x.Organizations).SingleOrDefault() ??
                       new User(null)
            {
                IsActive = true
            };
            var isSscAdmin = user.Roles.Any(x => x.Id == Role.Codes.SscAdmin);

            var model = new DepartmentalAdminModel
            {
                User       = user,
                IsSscAdmin = isSscAdmin
            };

            return(View(model));
        }
예제 #3
0
        public ActionResult ModifyDepartmental(DepartmentalAdminModel departmentalAdminModel, List <string> orgs)
        {
            if (orgs == null || orgs.Count == 0)
            {
                ModelState.AddModelError("User.Organizations", "You must select at least one department for a departmental Admin.");
            }
            if (!ModelState.IsValid)
            {
                return(View(departmentalAdminModel));
            }

            var user = _userRepository.GetNullableById(departmentalAdminModel.User.Id) ?? new User(departmentalAdminModel.User.Id);


            departmentalAdminModel.User.Roles = user.Roles;

            //Mapper.Map(departmentalAdminModel.User, user); // This was causing problems if an existing DA was saved.
            user.FirstName = departmentalAdminModel.User.FirstName;
            user.LastName  = departmentalAdminModel.User.LastName;
            user.Email     = departmentalAdminModel.User.Email;
            user.IsActive  = departmentalAdminModel.User.IsActive;

            var isDeptAdmin = user.Roles.Any(x => x.Id == Role.Codes.DepartmentalAdmin);
            var isSscAdmin  = user.Roles.Any(x => x.Id == Role.Codes.SscAdmin);

            if (!isDeptAdmin)
            {
                user.Roles.Add(_roleRepository.GetById(Role.Codes.DepartmentalAdmin));
            }

            user.Organizations = new List <Organization>();
            foreach (var org in orgs)
            {
                user.Organizations.Add(_organizationRepository.Queryable.Single(a => a.Id == org));
            }


            _userRepository.EnsurePersistent(user);

            // invalid the cache for the user that was just given permissions
            _userIdentity.RemoveUserRoleFromCache(Resources.Role_CacheId, user.Id);

            if (isSscAdmin && departmentalAdminModel.UpdateAllSscAdmins)
            {
                var userList = new List <string>();
                var users    = _roleRepository.Queryable.Where(x => x.Id == Role.Codes.SscAdmin).SelectMany(x => x.Users).Where(w => w.IsActive && w.Id != user.Id).ToList();

                foreach (var user1 in users)
                {
                    user1.Organizations = new List <Organization>();
                    foreach (var org in orgs)
                    {
                        user1.Organizations.Add(_organizationRepository.Queryable.Single(a => a.Id == org));
                    }
                    _userRepository.EnsurePersistent(user1);
                    // invalid the cache for the user that was just given permissions
                    _userIdentity.RemoveUserRoleFromCache(Resources.Role_CacheId, user1.Id);
                    userList.Add(user1.FullNameAndId);
                }
                Message =
                    string.Format(
                        "{0} was added as a departmental admin to the specified organization(s) Also added perms for {1}.",
                        user.FullNameAndId, string.Join(",", userList.ToArray()));
            }
            else
            {
                Message = string.Format("{0} was added as a departmental admin to the specified organization(s)",
                                        user.FullNameAndId);
            }

            return(this.RedirectToAction(a => a.Index()));
        }