Exemplo n.º 1
0
        public override async Task <Reply> UpdateName(UpdateNameRequest request, ServerCallContext context)
        {
            var reply = new Reply();

            if (!PwdEncryptor.ValidateUserName(request.NewName))
            {
                reply.Error = Error.InvalidArguments;
                return(reply);
            }

            var user = await _loginUserService.GetUser(context.GetHttpContext());

            if (user == null || user.Adm)
            {
                reply.Error = Error.InvalidOperation;
                return(reply);
            }

            var userEntity = await _usersService.All().FirstOrDefaultAsync(u => u.Id == user.Id);

            if (userEntity == null)
            {
                reply.Error = Error.InvalidOperation;
                return(reply);
            }

            if (userEntity.Id == request.NewName)
            {
                return(reply);
            }

            var existed = await _usersService.All().FirstOrDefaultAsync(u => u.Id == request.NewName);

            if (existed != null)
            {
                reply.Error = Error.InvalidOperation;
                return(reply);
            }

            userEntity.Id = request.NewName;
            await _usersService.Update(userEntity);

            user.Id = request.NewName;
            await _loginUserService.SetUser(context.GetHttpContext(), user);

            return(reply);
        }