コード例 #1
0
        private ConditionalApprovalModifyModel CreateModifyModel(string approvalType, ConditionalApprovalModifyModel existingModel = null)
        {
            var model = existingModel ?? new ConditionalApprovalModifyModel {
                ApprovalType = approvalType
            };

            var userWithOrgs = GetUserWithOrgs();

            if (approvalType == WorkgroupType)
            {
                model.Workgroups = GetWorkgroups(userWithOrgs).ToList();

                if (model.Workgroups.Count() == 0)
                {
                    return(null);
                }
            }
            else if (approvalType == OrganizationType)
            {
                model.Organizations = userWithOrgs.Organizations.ToList();

                if (model.Organizations.Count() == 0)
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }

            return(model);
        }
コード例 #2
0
        public ActionResult Create(int?workgroupId, string orgId, ConditionalApprovalModifyModel modifyModel)
        {
            if (workgroupId.HasValue)
            {
                ViewBag.WorkgroupId      = workgroupId.Value;
                modifyModel.ApprovalType = WorkgroupType;
                modifyModel.Workgroup    = _workgroupRepository.GetNullableById(workgroupId.Value);
                var workgroup = _workgroupRepository.Queryable.Single(a => a.Id == workgroupId.Value);
                if (workgroup.Administrative)
                {
                    ErrorMessage = "Conditional Approval may not be added to an administrative workgroup.";
                    return(this.RedirectToAction <WizardController>(a => a.Details(workgroup.Id)));
                }
            }
            else if (!string.IsNullOrWhiteSpace(orgId))
            {
                modifyModel.ApprovalType = OrganizationType;
                modifyModel.Organization = _organizationRepository.GetNullableById(orgId);
                ViewBag.OrganizationId   = orgId;
            }

            var primaryApproverInDb   = GetUserBySearchTerm(modifyModel.PrimaryApprover);
            var secondaryApproverInDb = string.IsNullOrWhiteSpace(modifyModel.SecondaryApprover)
                                            ? null
                                            : GetUserBySearchTerm(modifyModel.SecondaryApprover);

            if (primaryApproverInDb == null)
            {
                DirectoryUser primaryApproverInLdap = _directorySearchService.FindUser(modifyModel.PrimaryApprover);

                if (primaryApproverInLdap == null)
                {
                    ModelState.AddModelError("primaryapprover",
                                             "No user could be found with the kerberos or email address entered");
                }
                else //found the primary approver in ldap
                {
                    primaryApproverInDb = new User(primaryApproverInLdap.LoginId)
                    {
                        FirstName = primaryApproverInLdap.FirstName,
                        LastName  = primaryApproverInLdap.LastName,
                        Email     = primaryApproverInLdap.EmailAddress,
                        IsActive  = true
                    };

                    _userRepository.EnsurePersistent(primaryApproverInDb, forceSave: true);
                }
            }

            if (!string.IsNullOrWhiteSpace(modifyModel.SecondaryApprover)) //only check if a value was provided
            {
                if (secondaryApproverInDb == null)
                {
                    DirectoryUser secondaryApproverInLdap = _directorySearchService.FindUser(modifyModel.SecondaryApprover);

                    if (secondaryApproverInLdap == null)
                    {
                        ModelState.AddModelError("secondaryapprover",
                                                 "No user could be found with the kerberos or email address entered");
                    }
                    else //found the secondary approver in ldap
                    {
                        secondaryApproverInDb = new User(secondaryApproverInLdap.LoginId)
                        {
                            FirstName = secondaryApproverInLdap.FirstName,
                            LastName  = secondaryApproverInLdap.LastName,
                            Email     = secondaryApproverInLdap.EmailAddress,
                            IsActive  = true
                        };

                        _userRepository.EnsurePersistent(secondaryApproverInDb, forceSave: true);
                    }
                }
            }

            if (!ModelState.IsValid)
            {
                return(View(CreateModifyModel(modifyModel.ApprovalType, modifyModel)));
            }

            Check.Require(modifyModel.Workgroup != null || modifyModel.Organization != null, "Must have a Workgroup or an Organization");

            var newConditionalApproval = new ConditionalApproval
            {
                Question          = modifyModel.Question,
                Organization      = modifyModel.Organization,
                Workgroup         = modifyModel.Workgroup,
                PrimaryApprover   = primaryApproverInDb,
                SecondaryApprover = secondaryApproverInDb
            };

            _conditionalApprovalRepository.EnsurePersistent(newConditionalApproval);

            Message = "Conditional approval added successfully";

            if (workgroupId.HasValue)
            {
                return(this.RedirectToAction(a => a.ByWorkgroup(workgroupId.Value)));
            }

            if (!string.IsNullOrWhiteSpace(orgId))
            {
                return(this.RedirectToAction(a => a.ByOrg(orgId)));
            }

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