Exemplo n.º 1
0
        public JsonResult BlockUser(string blockedUID)
        {
            GenericResponse result = new GenericResponse()
            {
                IsSuccessful = false
            };

            try
            {
                if (blockedUID.IsNullOrWhitespace())
                {
                    result.ErrorMessage = "Bad request.";
                    return(new JsonResult(result));
                }

                // Retrieve the requested user
                User blocked = _userService.GetUserByUID(blockedUID);

                if (blocked != null && BlockedUserPermissionHelper.CanBlockUser(_blockedUserService, _context.CurrentUser, blocked))
                {
                    // Block the user
                    _blockedUserService.BlockUser(_context.CurrentUser, blocked);

                    // If we got this far we're successful
                    result.IsSuccessful = true;
                }
                else
                {
                    result.ErrorMessage = "You do not have permission to block this user.";
                }
            }
            catch (Exception e)
            {
                result.ErrorMessage = "An exception occurred.";
                _exceptionService.ReportException(e);
            }

            return(new JsonResult(result));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Block([FromBody] CreateBlockedUserModel model)
        {
            // Get authorized user.
            var blockingUser = await GetCurrentUserAsync();

            if (blockingUser is null ||
                blockingUser is default(User))
            {
                return(Unauthorized("No authorized user found.\nPlease log in by using your credentials."));
            }

            if (model is null)
            {
                return(BadRequest("Model cannot be null."));
            }

            if (string.IsNullOrEmpty(model.BlockedUserName))
            {
                return(BadRequest("BlockedUserName cannot be null."));
            }

            if (string.Equals(model.BlockedUserName, blockingUser.Username))
            {
                return(BadRequest("You cannot block yourself."));
            }

            var blockedUser = await _userService.GetUserByUsernameAsync(model.BlockedUserName);

            if (blockedUser is null)
            {
                return(NotFound("Blocked user not found."));
            }

            // Insert a new blockedUser.
            try
            {
                var result = await _blockedUserService.BlockUser(blockingUser, blockedUser);

                if (result is default(int))
                {
                    await _activityLogService.LogInvalidBlockedUserActivityAsync(new ActivityLog()
                    {
                        UserId  = blockingUser.Id,
                        Message = string.Format("{0} user not blocked by {1} user"
                                                , blockedUser.Id, blockingUser.Id)
                    });

                    return(Ok("User not blocked."));
                }
            }
            catch (Exception ex)
            {
                await _logService.LogErrorAsync(new CreateLogModel()
                {
                    UserId    = blockingUser.Id,
                    Title     = "BlockedUser Error",
                    Message   = "Error happened in BlockedUser Controller, Block function",
                    Exception = ex
                });

                return(Ok("User not blocked."));
            }

            await _activityLogService.LogBlockedUserActivityAsync(new ActivityLog()
            {
                UserId  = blockingUser.Id,
                Message = string.Format("{0} user blocked by {1} user ✔"
                                        , blockedUser.Id, blockingUser.Id)
            });

            return(Ok("User blocked ✔\nMessages coming from blocked user will not be showed up to you."));
        }