コード例 #1
0
        /// <summary>
        /// translate the account data coming from the client to a full user object
        /// </summary>
        /// <param name="account"></param>
        /// <param name="user"></param>
        private static void AccountToUser(Account account, IUser user)
        {
            user.EmailAddress = account.EmailAddress;
            user.DateOfBirth = account.DateOfBirth;
            user.Position = account.Position;
            user.PersonnelNumber = account.PersonnelNumber;
            var bitsOfName = account.Name.Split(' ');
            user.FirstName = bitsOfName[0];
            user.LastName = bitsOfName[1];
            user.Roles.Clear();
            if (account.IsAdministrator)
                user.Roles.Add(Role.GetRole(Role.BuiltInRole.Administrator));
            if (account.IsManager)
                user.Roles.Add(Role.GetRole(Role.BuiltInRole.Manager));
            if (account.IsAnalytics)
                user.Roles.Add(Role.GetRole(Role.BuiltInRole.Analytics));

            user.MobilePhoneNumber = account.MobilePhoneNumber;
            if (user.Avatar == null)
                user.Avatar = new Avatar { PublicID = account.PhotoId };
            else
                user.Avatar.PublicID = account.PhotoId;

            //user.NeedsAD = account.NeedsAD;

            user.CreatedDate = DateTime.UtcNow;
        }
コード例 #2
0
 private User CreateOrEditUser(Account account, bool isEdit)
 {
     var user = new User();
     if (isEdit)
     {
         using (var orgSession = OrgStore.OpenSession())
         {
             //we are editing a user so first get it from raven
             user = orgSession.Load<User>(account.Id);
             if (user == null)//cant find user in database
             {
                 GetLogger().Error(string.Format("Admin: EditOrCreateAccounts: Cannot find user {0}", account.Id));
                 //errorBuilder.Append("Cannot find user"); - what to do about this?
             }
         }
     }
     else
     {
         user.Id = IlluminateDatabase.GenerateId<User>();
         user.OrgKey = OrgKey;
     }
     return user;
 }
コード例 #3
0
 //private User UserADSettings(User user)
 //{
 //    if (user.NeedsAD && string.IsNullOrEmpty(user.ADUsername))
 //    {
 //        var adUserResult = ADHelper.UserToADUser(user);
 //        user.ADPassword = adUserResult.Password;
 //        user.ADUsername = adUserResult.Username;
 //    }
 //    if (!user.NeedsAD)
 //    {
 //        user.ADUsername = string.Empty;
 //        user.ADPassword = string.Empty;
 //    }
 //    return user;
 //}
 
 /// <summary>
 /// check a specific account is in the right format
 /// </summary>
 /// <param name="account"></param>
 /// <returns></returns>
 private static string CheckAccount(Account account)
 {
     var errors = new StringBuilder();
     if (string.IsNullOrEmpty(account.EmailAddress))
         errors.AppendFormat("Email address required");
     if (string.IsNullOrEmpty(account.Name))
         errors.AppendFormat("Users name required");
     else if (account.Name.Split(' ').Length != 2)
         errors.AppendFormat("Users name must be in the form First Name Last Name");
     return errors.ToString();
 }
コード例 #4
0
 public JsonResult AddAccounts(Account[] accounts)
 {
     return EditOrCreateAccounts(accounts, false);
 }
コード例 #5
0
        // need to verify that e-mail address given doesn't exist already in database.
        //used to both edit existing employees and add new ones
        private JsonResult EditOrCreateAccounts(Account[] accounts, bool isEdit)
        {
            //check the all the details are correct and if not send message to client
            var errors = CheckAccounts(accounts);
            if (errors.Length > 0)
                return JsonError(new AccountsAdminViewModel { ErrorMessage = errors }, errors);
            var users = new List<IUser>();
            var errorBuilder = new StringBuilder();
            // create/edit the users
            foreach (var account in accounts)
                {
                    var user = CreateOrEditUser(account, isEdit);
                    if (user == null && isEdit)//cant find user in database
                    {
                        errorBuilder.Append("Cannot find user");
                    }
                    using (var session = OrgStore.OpenSession())
                    {
                        if (session.Query<User>().Any(u => u.EmailAddress == account.EmailAddress) && !isEdit)
                        {
                            GetLogger().Error("Admin:EditOrCreateAccounts: attempted to add a user with an e-mail address already in OrgDB");
                            errorBuilder.Append("Cannot add user with this e-mail address. E-mail address already registered to another user");
                        }
                        else
                        {
                            //set the user details to that coming from the client
                            AccountToUser(account, user);// only do this if account.EmailAddress is not in DB, check master or OrgDB?
                        }
                    }
                    //user = UserADSettings(user);
                    //if (isEdit) continue;
                    // in original loop, user was saved here, but as only users being added are added to users, can loop through user list and add each one later.
                    users.Add(user);
                }
            if (errorBuilder.Length > 0)
                    return JsonError(new AccountsAdminViewModel { ErrorMessage = errorBuilder.ToString() }, errorBuilder.ToString());
            var vm = new AccountsAdminViewModel();
            // now open session here, first, store users
            using (var orgSession = OrgStore.OpenSession())
            {
                foreach (var user in users)
                {
                    orgSession.Store(user);
                }
                //save the changes to Raven and send a success message to the client
                try
                {
                    var shouldSave = true;
                    var editedAdded = "added";
                    if (isEdit)
                    {
                        editedAdded = "edited";
                    }
                    else
                    {
                        shouldSave=AddUsersToMasterDB(users);
                    }
                    if (shouldSave)
                    {
                        orgSession.SaveChanges();
                        vm.SuccessMessage = string.Format("Succesfully {0} {1} employee accounts", editedAdded,
                                                          accounts.Length);
                        return JsonSuccess(vm, vm.SuccessMessage);
                    }
                    else
                    {
                        GetLogger().Error("Admin:EditOrCreateAccounts: attempted to add a user with an e-mail address already in MasterDB");
                        vm.ErrorMessage = "Cannot add user with this e-mail address. E-mail address already registered to another user";
                        return JsonError(vm, vm.ErrorMessage);
                    }

                }
                catch (Exception ex)
                {
                    GetLogger().Error(string.Format("Admin:EditOrCreateAccounts: Cannot save changes to Raven"), ex);
                    vm.ErrorMessage = "Cannot save changes";
                    return JsonError(vm, vm.ErrorMessage);
                }
            }
        }
コード例 #6
0
        public JsonResult DeleteAccounts(Account[] accounts)
        {
            using (var orgSession = OrgStore.OpenSession())
            {
                var errorBuilder = new StringBuilder();
                foreach (var user in accounts.Select(account => orgSession.Load<User>(account.Id)))
                {
                    if (user == null)
                        errorBuilder.Append("Cannot find user");
                    if (user != null) user.IsDeleted = true;
                    // flag the membership reboot acount as closed.
                   
                    var userAccountService = MvcApplication.UserAccountService;
                    
                    var account = userAccountService.GetByEmail(user.EmailAddress);
                    userAccountService.DeleteAccount(account.Id);
                }
                if (errorBuilder.Length > 0)
                    return JsonError(new AccountsAdminViewModel { ErrorMessage = errorBuilder.ToString() }, errorBuilder.ToString());
                var vm = new AccountsAdminViewModel();

                //save the changes to Raven and send a success message to the client
                try
                {
                    orgSession.SaveChanges();
                    vm.SuccessMessage = string.Format("Succesfully soft deleted {0} employee accounts", accounts.Length);
                    return JsonSuccess(vm, vm.SuccessMessage);
                }
                catch (Exception ex)
                {
                    GetLogger().Error("Administration:Delete accounts:", ex);
                    vm.ErrorMessage = "Cannot delete accounts";
                    return JsonError(vm, vm.ErrorMessage);
                }
            }
        }
コード例 #7
0
 public JsonResult EditAccounts(Account[] accounts)
 {
     return EditOrCreateAccounts(accounts, true);
 }
コード例 #8
0
        public void AddAccountsNoName()
        {
            var controller = GetAuthenticatedControllerMock<AdministrationController>(Orguser);

            controller.ControllerContext = new ControllerContext(GetContext(false), new RouteData(), controller);

            var account = new Account
            {
                Id = "blah",
                EmailAddress = "blah"
            };
            var result = controller.AddAccounts(new[] { account });
            var jsonresponse = ValidateAndUnwrapJsonResult(result);

            var data = jsonresponse.Data as AccountsAdminViewModel;
            Assert.IsNotNull(data.ErrorMessage);
            Assert.IsNull(data.SuccessMessage);
            Assert.IsTrue(data.ErrorMessage.Contains("Users name required"));
            using (var session = OrgStore.OpenSession())
            {
                var newUser = session.Query<User>().FirstOrDefault(u => u.LastName == "Bunter");
                Assert.IsNull(newUser);

            }
        }
コード例 #9
0
        public void EditAccounts()
        {
            const string userId = "blah";
            var controller = GetAuthenticatedControllerMock<AdministrationController>(Orguser);

            controller.ControllerContext = new ControllerContext(GetContext(false), new RouteData(), controller);
            using (var session = OrgStore.OpenSession())
            {
                var user = new User
                               {
                                   Id = userId,
                                   FirstName = "Billy",
                                   LastName = "Bunter",
                                   EmailAddress = "sasas"
                               };
                session.Store(user);
                session.SaveChanges();
            }
            var account = new Account
            {
                Name = "Bob Baker",
                EmailAddress = "wwewe",
                Id = userId
            };
            var result = controller.EditAccounts(new[] { account });
            var jsonresponse = ValidateAndUnwrapJsonResult(result);

            var data = jsonresponse.Data as AccountsAdminViewModel;
            Assert.IsNull(data.ErrorMessage);
            Assert.IsNotNull(data.SuccessMessage);
            using (var session = OrgStore.OpenSession())
            {
                var changedUser = session.Load<User>(userId);
                Assert.IsNotNull(changedUser);
                Assert.AreEqual(account.EmailAddress, changedUser.EmailAddress);
                Assert.AreEqual(account.Name, changedUser.Name);
            }
        }
コード例 #10
0
        public void AddAccountsGood()
        {
            var controller = GetAuthenticatedControllerMock<AdministrationController>(Orguser);

            controller.ControllerContext = new ControllerContext(GetContext(false), new RouteData(), controller);

            var account = new Account
                              {
                                  Name = "Bill Bunter",
                                  EmailAddress = "*****@*****.**",
                                  Id = "blah"
                              };
            var result=controller.AddAccounts(new [] {account});
            var jsonresponse = ValidateAndUnwrapJsonResult(result);

            var data = jsonresponse.Data as AccountsAdminViewModel;
            Assert.IsNull(data.ErrorMessage);
            Assert.IsNotNull(data.SuccessMessage);
            using (var session = OrgStore.OpenSession())
            {
                var newUser=session.Query<User>().FirstOrDefault(u => u.LastName == "Bunter");
                Assert.IsNotNull(newUser);
                Assert.AreEqual(account.EmailAddress,newUser.EmailAddress);
                Assert.AreEqual(session.Query<User>().Count(u => u.LastName == "Bunter"), 1);
            }
        }