public ActionResult Create(School school)
        {
            try
            {
                using (var context = new Context())
                {
                    context.Schools.Add(school);
                    context.SaveChanges();

                    var account = new Account()
                    {
                        LoginName = "school" + school.Id.ToString() + "admin",
                        Password = "******",
                        SchoolId = school.Id,
                        Role = (int) Role.Admin,
                        DisplayName = school.Name + " admin"
                    };
                    context.Accounts.Add(account);
                    context.SaveChanges();

                    TempData["message"] = "New admin account: " + account.LoginName + " " + account.Password;
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        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 AccountViewModel(Account acc)
 {
     Id = acc.Id;
     LoginName = (acc.LoginName == null) ? "" : acc.LoginName;
     DisplayName = (acc.DisplayName == null) ? "" : acc.DisplayName;
     LastLogin = acc.LastLogin;
     RoleEnum = acc.RoleEnum;
 }
        /// <summary>
        /// Create a new organization or update an existing one.
        /// This method will never be called if the organization.Id is null (for example, if the attribute
        /// has not been requested from RM Unify).
        /// If your app stores information about an organization, it should use organization.Id as a key to create a
        /// new organization record or update an existing one.
        /// </summary>
        /// <param name="organization">Organization profile</param>
        /// <param name="source">Source of update (sign on or provisioning)</param>
        public override void CreateOrUpdateUser(RmUnifyUser rmUser, Source source)
        {
            /// This override supports giving each user a login name that is suitable for display
            if (_school == null)
            {
                throw new Exception("CreateOrUpdateUser() called before CreateOrUpdateOrganization()");
            }

            using (var context = new Context())
            {
                Account account = (from a in context.Accounts
                                   where a.RmUnifyId == rmUser.Id
                                   select a).SingleOrDefault();
                if (account == null)
                {
                    account = new Account()
                    {
                        RmUnifyId = rmUser.Id,
                        SchoolId = _school.Id,
                        Password = Guid.NewGuid().ToString() // use random unguessable password
                    };
                    context.Accounts.Add(account);
                }
                else
                {
                    if (account.SchoolId != _school.Id)
                    {
                        // If you use rmUnifyUser.PersonId, you will need to support a user moving between schools
                        // If you use rmUnifyUser.Id, a user moving between schools will be assigned a new Id
                        throw new Exception("School moves not supported");
                    }
                }

                account.LoginName = DeDupeLoginName(rmUser.UnifyUserName, rmUser.Id);
                account.DisplayName = rmUser.DisplayName;
                account.RoleEnum = GetRole(rmUser);
                account.DeletedDate = null;  // if previously deleted, restore
                if (source == Source.SingleSignOn)
                {
                    account.LastLogin = DateTime.Now;
                }
                context.SaveChanges();

                // Cache account for next method
                _account = account;
            }

            // Purge any old users from the system
            // In this example, we've chosen to implement this in the login process
            // If this is likely to be long running, we might want to implement it as a background task
            PurgeUsers();
        }