public ActionResult CreateWorkgroup(Workgroup workgroup)
        {
            ViewBag.StepNumber = 1;

            var user = GetCurrentUser();

            //if(!user.Organizations.Any(a => a.Id == workgroup.PrimaryOrganization.Id))
            //{
            //    ModelState.AddModelError("Workgroup.PrimaryOrganization", "You do not have access to the selected organization");
            //}
            // takes into account department traversal down
            string message;

            if (!_securityService.HasWorkgroupOrOrganizationAccess(null, workgroup.PrimaryOrganization, out message))
            {
                ModelState.AddModelError("Workgroup.PrimaryOrganization", "You do not have access to the selected organization");
            }

            if (workgroup.Administrative && workgroup.SyncAccounts)
            {
                ModelState.AddModelError("Workgroup.Administrative", "Can not have both Administrative and Sync Accounts selected.");
            }

            if (workgroup.IsFullFeatured && !workgroup.Administrative)
            {
                ModelState.AddModelError("Workgroup.Administrative", "If Full Featured, workgroup must be administrative.");
            }

            if (workgroup.Administrative && !string.IsNullOrEmpty(workgroup.NotificationEmailList))
            {
                ModelState.AddModelError("Workgroup.NotificationEmailList", "Notification email list will not do anything for an administrative group.");
            }

            if (workgroup.Administrative && workgroup.DoNotInheritPermissions)
            {
                ModelState.AddModelError("Workgroup.DoNotInheritPermissions", "Can not have both Administrative and Do Not Inherit Permissions selected.");
            }



            if (!ModelState.IsValid)
            {
                var model = WorkgroupModifyModel.Create(user, _queryRepositoryFactory);
                model.Workgroup = workgroup;

                return(View(model));
            }

            var createdWorkgroup = _workgroupService.CreateWorkgroup(workgroup, null);

            Message = string.Format("{0} workgroup was created",
                                    createdWorkgroup.Name);

            return(this.RedirectToAction(a => a.AddSubOrganizations(createdWorkgroup.Id)));
        }
        public void TestCreateWorkgroupAddsPrimaryOrgAsASubOrg()
        {
            #region Arrange
            var workgroup = CreateValidEntities.Workgroup(1);
            workgroup.PrimaryOrganization = CreateValidEntities.Organization(9);
            workgroup.Organizations       = new List <Organization>();

            new FakeRelatedWorkgroups(3, QueryRepositoryFactory.RelatatedWorkgroupsRepository);
            new FakeWorkgroupPermissions(3, WorkgroupPermissionRepository);
            #endregion Arrange

            #region Act
            var result = WorkgroupService.CreateWorkgroup(workgroup, null);
            #endregion Act

            #region Assert
            Assert.IsNotNull(result);
            Assert.AreEqual("Name9", result.PrimaryOrganization.Name);
            Assert.AreEqual(1, result.Organizations.Count());
            Assert.AreEqual("Name9", result.Organizations[0].Name);
            WorkgroupRepository.AssertWasCalled(a => a.EnsurePersistent(result));
            #endregion Assert
        }