示例#1
0
        public async Task AdminUpdateUserWithAllPropertiesNull()
        {
            UserUpdateInfo info = new UserUpdateInfo();
            var            ret  = await _client.AdminUpdateUser(_userToRename.Id, info);

            Assert.IsTrue(ret.HasError);
            Assert.IsFalse(ret.HasValue);
            Assert.IsTrue(ret.Error is ArgumentNullException);
        }
示例#2
0
        public async Task AdminUpdateUserUsername()
        {
            UserUpdateInfo info = new UserUpdateInfo()
            {
                Username = "******"
            };
            var ret = await _client.AdminUpdateUser(_userToRename.Id, info);

            Assert.IsFalse(ret.HasError);
            Assert.IsTrue(ret.HasValue);
            Assert.AreEqual("RENAMED", ret.Value.Username);
        }
 public IActionResult UpdateAccountInfo([FromBody] UserUpdateInfo userInfo)
 {
     try
     {
         var info = authenticationService.UpdateUserInfo(userInfo);
         return(Ok(info));
     }
     catch (Exception e)
     {
         return(BadRequest());
     }
 }
示例#4
0
        /// <inheritdoc />
        public async Task <Maybe <UserInfoDto> > AdminUpdateUser(int userId, UserUpdateInfo updateInfo)
        {
            if (!updateInfo.HasAtLeastOneProperty())
            {
                // All of the properties are null
                var ex = new ArgumentNullException(nameof(updateInfo), "Update info must have at LEAST one non null property");
                return(Maybe.FromErr <UserInfoDto>(ex));
            }

            // Otherwise send the request
            return(await this.GetAndMapResponse <UserInfoDto>($"{Endpoints.AdminUpdateUser}/{userId.ToString()}", HttpMethods.Put, updateInfo)
                   .ConfigureAwait(false));
        }
        public async Task <ActionResult> UpdateAccount([FromBody] UserUpdateInfo userUpdateInfo)
        {
            //TODO:Check authorization
            if (string.IsNullOrEmpty(userUpdateInfo.Id))
            {
                userUpdateInfo.Id = WorkContext.CurrentUser.Id;
            }
            var isSelfEditing = userUpdateInfo.Id == WorkContext.CurrentUser.Id;


            if (!string.IsNullOrEmpty(userUpdateInfo.Id))
            {
                var user = await _userManager.FindByIdAsync(userUpdateInfo.Id);

                if (user != null)
                {
                    if (!isSelfEditing)
                    {
                        var authorizationResult = await _authorizationService.AuthorizeAsync(User, null, SecurityConstants.Permissions.CanEditUsers);

                        if (authorizationResult.Succeeded)
                        {
                            authorizationResult = await _authorizationService.AuthorizeAsync(User, user?.Contact?.Organization, CanEditOrganizationResourceAuthorizeRequirement.PolicyName);
                        }
                        if (!authorizationResult.Succeeded)
                        {
                            return(Unauthorized());
                        }
                        //Doesn't allow change self roles
                        user.Roles = userUpdateInfo.Roles?.Select(x => new Role {
                            Id = x, Name = x
                        });
                    }

                    if (user.Contact != null)
                    {
                        user.Contact.FirstName = userUpdateInfo.FirstName;
                        user.Contact.LastName  = userUpdateInfo.LastName;
                        user.Contact.FullName  = userUpdateInfo.FullName;
                        user.Contact.DefaultShippingAddress = userUpdateInfo.DefaultShippingAddress;
                        user.Contact.DefaultBillingAddress  = userUpdateInfo.DefaultBillingAddress;
                        user.Contact.Title = userUpdateInfo.Title;
                    }

                    user.Email = userUpdateInfo.Email;

                    await _userManager.UpdateAsync(user);
                }
            }
            return(Ok());
        }
        private UpdateResult UpdateResult(IIdentity actor, IUser target, bool store, UserUpdateInfo updateinfo
          ) {
            UpdateResult res = null;
            Logg.Info(new {updateusr = "******", usr = actor.Name, info = updateinfo}.stringify());
            if (string.IsNullOrWhiteSpace(updateinfo.Login)) {
                updateinfo.Login = actor.Name;
            }
            target = target ?? Users.GetUser(updateinfo.Login);
            var newuser = target == null;
            if (!updateinfo.HasDelta(target)) {
                return new UpdateResult {Ok = true, Message="notchanged", ResultUser = target};
            }

            res = Checker.ValidateUpdate(actor, updateinfo, target);
            if (!res.Ok) {
                Logg.Warn(
                    new {updateusr = "******", validation = res, usr = actor.Name, info = updateinfo}.stringify());

                return res;
            }
            if (null == target) {
                target = new User {Login = updateinfo.Login};
            }
            updateinfo.Apply(target);
            UserPolicy policy = null;
            if (newuser) {
                policy = UserPolicy.GetNewUserPolicy(updateinfo);
            }
            if (null != policy) {
                policy.Apply(target);
            }
            if (store) {
                Users.Store(target);
            }
            if (null != policy && policy.MakePassRequest && null != Messendger) {
                if (newuser) {
                    Messendger.SendWelcome(target);
                }
            }
            Logg.Info(new {updateusr = "******", result = target, usr = actor.Name, info = updateinfo}.stringify());
            Users.Clear();
            Roles.Clear();
            res = new UpdateResult {Ok = true, ResultUser = target};
            return res;
        }
        public UserUpdateInfo UpdateUserInfo(UserUpdateInfo userInfo)
        {
            var user = this._context.Users.Where(data => data.UserId == userInfo.UserId).SingleOrDefault();

            if (user != null)
            {
                user.Name    = userInfo.Name;
                user.Address = userInfo.Address;
                user.DOB     = userInfo.DOB;

                _context.Entry(user).State = EntityState.Modified;
                _context.SaveChanges();
                return(new UserUpdateInfo
                {
                    UserId = user.UserId,
                    Name = user.Name,
                    Address = user.Address,
                    DOB = user.DOB
                });
            }
            throw new Exception();
        }
示例#8
0
        private void DoTest(bool expectedResult, string callerName, UserUpdateInfo update = null, User existed = null)
        {
            update = update ?? new UserUpdateInfo();
            if (string.IsNullOrEmpty(update.Login))
            {
                update.Login = "******";
            }
            userSource.Remove(Identity.GetIdentityKey(update.Login));
            if (null != existed)
            {
                userSource.Store(existed); //setup as update
            }
            var me     = users.GetUser(callerName);
            var result = updateChecker.ValidateUpdate(new Identity(me)
            {
                IsAuthenticated = true
            }, update, existed);

            if (!result.Ok)
            {
                Console.WriteLine(result.stringify());
            }
            Assert.AreEqual(expectedResult, result.Ok);
        }
 public UpdateResult[] DefineUser(IIdentity actor, UserUpdateInfo[] updateinfos, IUser target = null,
     bool store = true) {
     return  updateinfos.Select(_=>UpdateResult(actor, target, store, _)).ToArray();
 }
示例#10
0
 public UserUpdateInfo UpdateUserInfo(UserUpdateInfo userInfo)
 {
     return(userRepository.UpdateUserInfo(userInfo));
 }
示例#11
0
        public async Task <ActionResult> ChangeUserInformation([FromForm] UserUpdateInfo userData)
        {
            using (var db = ArangoDatabase.CreateWithSetting())
            {
                var user = await(from u in db.Query <User>()
                                 where u.Key == HttpContext.User.Identity.Name
                                 select u).FirstOrDefaultAsync();

                // no user exists
                if (user == null)
                {
                    return(NotFound());
                }

                user.Password = userData.Password != null
                    ? PasswordHasher.HashPassword(user, userData.Password)
                    : user.Password;

                user.RealName               = userData.RealName ?? user.RealName;
                user.Description            = userData.Description ?? user.Description;
                user.Birthday               = userData.Birthday ?? user.Birthday;
                user.Occupation             = userData.Occupation ?? user.Occupation;
                user.NotificationPreference = userData.Preferences ?? user.NotificationPreference;
                if (userData.ProfilePicture != null || userData.ProfilePictureAsBase64 != null)
                {
                    var blob = Container.GetBlockBlobReference($"users/{user.Key}/profile.jpeg");
                    if (userData.ProfilePicture != null)
                    {
                        try
                        {
                            await blob.UploadFromStreamAsync(userData.ProfilePicture.OpenReadStream());
                        }
                        catch (Exception e)
                        {
                            Console.Error.WriteLine(e.Message);
                            return(StatusCode(500));
                        }
                    }
                    else
                    {
                        try
                        {
                            var data = Convert.FromBase64String(userData.ProfilePictureAsBase64);
                            await blob.UploadFromByteArrayAsync(data, 0, data.Length);
                        }
                        catch (Exception e)
                        {
                            Console.Error.WriteLine(e);
                            return(StatusCode(500));
                        }
                    }

                    await blob.FetchAttributesAsync();

                    blob.Properties.ContentType = "image/jpeg";
                    await blob.SetPropertiesAsync();
                }

                await db.UpdateByIdAsync <User>(user.Key, user);

                return(Ok());
            }
        }