Beispiel #1
0
        public Rock.CRM.DTO.Person ApiGet(string id, string apiKey)
        {
            using (Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope())
            {
                Rock.CMS.UserService userService = new Rock.CMS.UserService();
                Rock.CMS.User        user        = userService.Queryable().Where(u => u.ApiKey == apiKey).FirstOrDefault();

                if (user != null)
                {
                    uow.objectContext.Configuration.ProxyCreationEnabled = false;
                    Rock.CRM.PersonService PersonService = new Rock.CRM.PersonService();
                    Rock.CRM.Person        Person        = PersonService.Get(int.Parse(id));
                    if (Person.Authorized("View", user))
                    {
                        return(Person.DataTransferObject);
                    }
                    else
                    {
                        throw new WebFaultException <string>("Not Authorized to View this Person", System.Net.HttpStatusCode.Forbidden);
                    }
                }
                else
                {
                    throw new WebFaultException <string>("Invalid API Key", System.Net.HttpStatusCode.Forbidden);
                }
            }
        }
Beispiel #2
0
        public void UpdatePerson(string id, Rock.CRM.DTO.Person Person)
        {
            var currentUser = Rock.CMS.UserService.GetCurrentUser();

            if (currentUser == null)
            {
                throw new WebFaultException <string>("Must be logged in", System.Net.HttpStatusCode.Forbidden);
            }

            using (Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope())
            {
                uow.objectContext.Configuration.ProxyCreationEnabled = false;
                Rock.CRM.PersonService PersonService  = new Rock.CRM.PersonService();
                Rock.CRM.Person        existingPerson = PersonService.Get(int.Parse(id));
                if (existingPerson.Authorized("Edit", currentUser))
                {
                    uow.objectContext.Entry(existingPerson).CurrentValues.SetValues(Person);

                    if (existingPerson.IsValid)
                    {
                        PersonService.Save(existingPerson, currentUser.PersonId);
                    }
                    else
                    {
                        throw new WebFaultException <string>(existingPerson.ValidationResults.AsDelimited(", "), System.Net.HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    throw new WebFaultException <string>("Not Authorized to Edit this Person", System.Net.HttpStatusCode.Forbidden);
                }
            }
        }
Beispiel #3
0
        public void DeletePerson(string id)
        {
            var currentUser = Rock.CMS.UserService.GetCurrentUser();

            if (currentUser == null)
            {
                throw new WebFaultException <string>("Must be logged in", System.Net.HttpStatusCode.Forbidden);
            }

            using (Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope())
            {
                uow.objectContext.Configuration.ProxyCreationEnabled = false;
                Rock.CRM.PersonService PersonService = new Rock.CRM.PersonService();
                Rock.CRM.Person        Person        = PersonService.Get(int.Parse(id));
                if (Person.Authorized("Edit", currentUser))
                {
                    PersonService.Delete(Person, currentUser.PersonId);
                    PersonService.Save(Person, currentUser.PersonId);
                }
                else
                {
                    throw new WebFaultException <string>("Not Authorized to Edit this Person", System.Net.HttpStatusCode.Forbidden);
                }
            }
        }
Beispiel #4
0
        public void ApiCreatePerson(string apiKey, Rock.CRM.DTO.Person Person)
        {
            using (Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope())
            {
                Rock.CMS.UserService userService = new Rock.CMS.UserService();
                Rock.CMS.User        user        = userService.Queryable().Where(u => u.ApiKey == apiKey).FirstOrDefault();

                if (user != null)
                {
                    uow.objectContext.Configuration.ProxyCreationEnabled = false;
                    Rock.CRM.PersonService PersonService  = new Rock.CRM.PersonService();
                    Rock.CRM.Person        existingPerson = new Rock.CRM.Person();
                    PersonService.Add(existingPerson, user.PersonId);
                    uow.objectContext.Entry(existingPerson).CurrentValues.SetValues(Person);

                    if (existingPerson.IsValid)
                    {
                        PersonService.Save(existingPerson, user.PersonId);
                    }
                    else
                    {
                        throw new WebFaultException <string>(existingPerson.ValidationResults.AsDelimited(", "), System.Net.HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    throw new WebFaultException <string>("Invalid API Key", System.Net.HttpStatusCode.Forbidden);
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Creates a new user.
        /// </summary>
        /// <param name="person">The person.</param>
        /// <param name="authenticationType">Type of the authentication.</param>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <param name="isConfirmed">if set to <c>true</c> [is confirmed].</param>
        /// <param name="currentPersonId">The current person id.</param>
        /// <returns></returns>
        public User Create(Rock.CRM.Person person,
                           AuthenticationType authenticationType,
                           string username,
                           string password,
                           bool isConfirmed,
                           int?currentPersonId)
        {
            User user = this.GetByUserName(username);

            if (user != null)
            {
                throw new ArgumentOutOfRangeException("username", "Username already exists");
            }

            DateTime createDate = DateTime.Now;

            user                         = new User();
            user.UserName                = username;
            user.Password                = EncodePassword(password);
            user.IsConfirmed             = isConfirmed;
            user.CreationDate            = createDate;
            user.LastPasswordChangedDate = createDate;
            if (person != null)
            {
                user.PersonId = person.Id;
            }
            user.AuthenticationType = authenticationType;

            this.Add(user, currentPersonId);
            this.Save(user, currentPersonId);

            return(user);
        }
Beispiel #6
0
        /// <summary>
        /// Gets the current user.
        /// </summary>
        /// <param name="userIsOnline">if set to <c>true</c> [user is online].</param>
        /// <returns></returns>
        public static User GetCurrentUser(bool userIsOnline)
        {
            string userName = User.GetCurrentUserName();

            if (userName != string.Empty)
            {
                if (userName.StartsWith("rckipid="))
                {
                    Rock.CRM.PersonService personService      = new CRM.PersonService();
                    Rock.CRM.Person        impersonatedPerson = personService.GetByEncryptedKey(userName.Substring(8));
                    if (impersonatedPerson != null)
                    {
                        return(impersonatedPerson.ImpersonatedUser);
                    }
                }
                else
                {
                    UserService userService = new UserService();
                    User        user        = userService.GetByUserName(userName);

                    if (user != null && userIsOnline)
                    {
                        user.LastActivityDate = DateTime.Now;
                        userService.Save(user, null);
                    }

                    return(user);
                }
            }

            return(null);
        }
Beispiel #7
0
        public Rock.CRM.DTO.Person Get(string id)
        {
            var currentUser = Rock.CMS.UserService.GetCurrentUser();

            if (currentUser == null)
            {
                throw new WebFaultException <string>("Must be logged in", System.Net.HttpStatusCode.Forbidden);
            }

            using (Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope())
            {
                uow.objectContext.Configuration.ProxyCreationEnabled = false;
                Rock.CRM.PersonService PersonService = new Rock.CRM.PersonService();
                Rock.CRM.Person        Person        = PersonService.Get(int.Parse(id));
                if (Person.Authorized("View", currentUser))
                {
                    return(Person.DataTransferObject);
                }
                else
                {
                    throw new WebFaultException <string>("Not Authorized to View this Person", System.Net.HttpStatusCode.Forbidden);
                }
            }
        }