Exemple #1
0
        public ApplicationUser GetUserForEntityrWhoIsNotAdminFor(string entityId)
        {
            //get the entity
            TEntity entity = Find(entityId);

            entity.IsNullThrowException("Entity not found");

            PlayerAbstract playerAbstract = entity as PlayerAbstract;

            playerAbstract.IsNullThrowException("playerAbstract");

            string personId = entity.PersonId;

            personId.IsNullOrWhiteSpaceThrowArgumentException("personId");
            Person person = PersonBiz.Find(personId);

            person.IsNullThrowException("No person found");

            //Now get the user. Here we can get multiple users if person is being impersonated by admin.
            //we have to deal with that.

            List <ApplicationUser> usersForPerson = UserBiz.FindAll().Where(x => x.PersonId == personId).ToList();

            usersForPerson.IsNullOrEmptyThrowException("Person not found");

            //if it is the admininistrators own account, there will be only one person and the item will get thru
            if (usersForPerson.Count() == 1)
            {
                return(usersForPerson[0]);
            }


            //Getting rid of administrators

            //if more than 1 person has been found then the admin is impersonating this person
            //at this time.
            //get rid of all administrators.

            //find all administrators
            List <ApplicationUser> allAdminList = UserBiz.GetAllAdmin();

            //create a duplicate list of usersForPerson we will use for itteration
            List <ApplicationUser> duplicate_usersForPerson = new List <ApplicationUser>();;

            foreach (ApplicationUser user in usersForPerson)
            {
                duplicate_usersForPerson.Add(user);
            }

            //now itterate thru duplicate_usersForPerson and remove admins from usersForPerson
            foreach (var user in duplicate_usersForPerson)
            {
                if (allAdminList.Contains(user))
                {
                    usersForPerson.Remove(user);
                }
            }

            //all administrators are now gone from usersForPerson
            usersForPerson.IsNullOrEmptyThrowException("Person not found");
            if (usersForPerson.Count() > 1)
            {
                throw new Exception("There is more than one person for the user. This is a data error.");
            }
            return(usersForPerson[0]);
        }