示例#1
0
        public ActionResult ManageCompany()
        {
            ManageCompanyScopesModel viewModel = new ManageCompanyScopesModel();
            viewModel.CompanyId = _service.GetUser(_security.GetUserId(User.Identity.Name)).CompanyId;

            return View(viewModel);
        }
示例#2
0
        public ActionResult ManageCompany(ManageCompanyScopesModel viewModel)
        {
            if (ModelState.IsValid)
            {
                // get list of existing selections
                List<Scope> existing = _service.GetEnumerableForCompany(viewModel.CompanyId).ToList();

                Scope theScope;
                // add selections not in existing collection
                var toAdd = viewModel.SelectedScope.Where(x => !existing.Select(s => s.Id).Contains(x));
                foreach (var a in toAdd)
                {
                    // get the scope from the back end
                    theScope = _service.Get(a);
                    // add company associatoin to the scope
                    theScope.Companies.Add(new CompanyXScope { CompanyId = viewModel.CompanyId, Scope = theScope });
                    // save changes
                    if (!_service.Update(theScope))
                    {
                        throw new HttpException(500, _service.ValidationDic.First().Value);
                    }
                }

                // remove scopes not in selected that are in existing
                var toRemove = existing.Where(y => !viewModel.SelectedScope.Contains(y.Id)).Select(s => s.Id);
                foreach (var r in toRemove)
                {
                    // find the scope in the list of existing selections
                    theScope = existing.Where(x => x.Id == r).First();
                    // find the company in the scopes companies
                    var xCompany = theScope.Companies.Where(x => x.CompanyId == viewModel.CompanyId).FirstOrDefault();
                    // if company is found.  should always be found
                    if (xCompany != null)
                    {
                        // remove the company from the scope
                        theScope.Companies.Remove(xCompany);
                        // save the changes
                        if (!_service.Update(theScope))
                        {
                            throw new HttpException(500, _service.ValidationDic.First().Value);
                        }
                    }
                }
                return RedirectToRoute("Default", new { controller = "Account", action = "Manage" });
            }
            return View(viewModel);
        }