public ActionResult Create(AccountViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (var context = new Context())
                {
                    Account oldAccount = (from a in context.Accounts
                                          where a.LoginName == model.LoginName
                                          select a).SingleOrDefault();
                    if (oldAccount != null)
                    {
                        ModelState.AddModelError("LoginName", "Login name already in use");
                    }
                    else
                    {
                        Account account = new Account()
                        {
                            LoginName = model.LoginName,
                            Password = model.Password,
                            DisplayName = model.DisplayName,
                            RoleEnum = model.RoleEnum,
                            SchoolId = School.Current.Id
                        };
                        context.Accounts.Add(account);
                        context.SaveChanges();

                        return RedirectToAction("Index");
                    }
                }
            }

            model.CanEditDisplayName = model.CanEditLoginName = model.CanEditRole = model.CanEditPassword = true;
            return View(model);
        }
 public ActionResult Index()
 {
     var model = new AccountViewModel(Account.Current);
     model.CanEditDisplayName = CanEditDisplayName();
     ViewBag.CancelController = "Blog";
     return View(model);
 }
        public ActionResult Index(AccountViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (CanEditDisplayName())
                {
                    using (var context = new Context())
                    {
                        Account account = (from a in context.Accounts
                                           where a.Id == Account.Current.Id
                                           select a).FirstOrDefault();
                        account.DisplayName = model.DisplayName;
                        context.SaveChanges();
                    }
                }

                return RedirectToAction("Index", "Blog");
            }

            return Index();
        }
 public ActionResult Create()
 {
     AccountViewModel model = new AccountViewModel(new Account());
     model.CanEditDisplayName = model.CanEditLoginName = model.CanEditRole = model.CanEditPassword = true;
     return View(model);
 }
        public ActionResult Edit(int id, AccountViewModel model)
        {
            // RMUNIFY
            if (School.Current.IsRmUnifySchool)
            {
                throw new Exception("Can't edit users in RM Unify schools");
            }
            // END RMUNIFY

            model.CanEditDisplayName = true;
            model.CanEditLoginName = model.CanEditRole = (id != Account.Current.Id);

            if (ModelState.IsValid)
            {
                using (var context = new Context())
                {
                    Account oldAccount = (from a in context.Accounts
                                          where a.LoginName == model.LoginName
                                          select a).SingleOrDefault();
                    if (oldAccount != null)
                    {
                        ModelState.AddModelError("LoginName", "Login name already in use");
                    }
                    else
                    {
                        Account account = (from a in context.Accounts
                                           where a.Id == id && a.SchoolId == School.Current.Id
                                             && a.DeletedDate == null
                                           select a).SingleOrDefault();

                        // account not found or not in school
                        if (account == null)
                        {
                            return this.HttpNotFound();
                        }

                        if (model.CanEditLoginName)
                        {
                            account.LoginName = model.LoginName;
                        }
                        if (model.CanEditDisplayName)
                        {
                            account.DisplayName = model.DisplayName;
                        }
                        if (model.CanEditRole)
                        {
                            account.RoleEnum = model.RoleEnum;
                        }
                        context.SaveChanges();
                    }
                }

                return RedirectToAction("Index");
            }

            return View(model);
        }
        public ActionResult Edit(int id)
        {
            using (var context = new Context())
            {
                Account account = (from a in context.Accounts
                                   where a.Id == id && a.SchoolId == School.Current.Id
                                     && a.DeletedDate == null
                                   select a).SingleOrDefault();

                // account not found or not in school
                if (account == null)
                {
                    return this.HttpNotFound();
                }

                var model = new AccountViewModel(account);

                model.CanEditDisplayName = true;
                model.CanEditLoginName = model.CanEditRole = (id != Account.Current.Id);
                return View(model);
            }
        }