Exemple #1
0
        public void UpdateUser(string id, Rock.CMS.DTO.User User)
        {
            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.CMS.UserService UserService  = new Rock.CMS.UserService();
                Rock.CMS.User        existingUser = UserService.Get(int.Parse(id));
                if (existingUser.Authorized("Edit", currentUser))
                {
                    uow.objectContext.Entry(existingUser).CurrentValues.SetValues(User);

                    if (existingUser.IsValid)
                    {
                        UserService.Save(existingUser, currentUser.PersonId);
                    }
                    else
                    {
                        throw new WebFaultException <string>(existingUser.ValidationResults.AsDelimited(", "), System.Net.HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    throw new WebFaultException <string>("Not Authorized to Edit this User", System.Net.HttpStatusCode.Forbidden);
                }
            }
        }
Exemple #2
0
        public void ApiCreateUser(string apiKey, Rock.CMS.DTO.User User)
        {
            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.CMS.UserService UserService  = new Rock.CMS.UserService();
                    Rock.CMS.User        existingUser = new Rock.CMS.User();
                    UserService.Add(existingUser, user.PersonId);
                    uow.objectContext.Entry(existingUser).CurrentValues.SetValues(User);

                    if (existingUser.IsValid)
                    {
                        UserService.Save(existingUser, user.PersonId);
                    }
                    else
                    {
                        throw new WebFaultException <string>(existingUser.ValidationResults.AsDelimited(", "), System.Net.HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    throw new WebFaultException <string>("Invalid API Key", System.Net.HttpStatusCode.Forbidden);
                }
            }
        }