コード例 #1
0
        public async Task <IActionResult> SendLogin(string username, string password, string ReturnUrl)
        {
            // Returns error if username or password are null
            if (username == null || password == null)
            {
                return(Redirect("/Login?error=0"));
            }

            // Retrieves user, returning error if none are found
            User user = await _repository.GetUserAsync(username);

            if (user == null)
            {
                return(Redirect("/Login?error=1"));
            }

            // Returns error if password is incorrect
            if (!user.CheckPassword(password) || user.Deleted)
            {
                return(Redirect("/Login?error=1"));
            }
            IActionResult returnResult = Url.IsLocalUrl(ReturnUrl) ? Redirect(ReturnUrl) : Redirect("/");

            return(await SignInUser(user, returnResult));
        }
コード例 #2
0
        public async Task <IActionResult> CreateThread(CreateThreadRequest request)
        {
            // Returns error if either parameter is null
            if (request.Title == null || request.Content == null)
            {
                return(BadRequest("Title and content cannot be null"));
            }

            // Creates and adds thread to database
            User user = await _repository.GetUserAsync(User);

            Thread thread = new Thread
            {
                Title      = request.Title,
                Content    = request.Content,
                DatePosted = DateTime.Now,
                User       = user
            };
            await _repository.AddThreadAsync(thread);

            await _repository.SaveChangesAsync();

            // Returns JSON response
            return(Json(new ApiThread(thread)));
        }
コード例 #3
0
        public async Task <IActionResult> GetUser(int id)
        {
            User user = await _repository.GetUserAsync(id);

            if (user == null)
            {
                return(NotFound("User not found"));
            }
            if (user.Deleted)
            {
                return(Gone("User deleted"));
            }

            return(Json(new ApiUser(user)));
        }
コード例 #4
0
        public async Task <IEnumerable <ApiNotification> > GetNotifications()
        {
            // Retrieves user and returns notifications
            User user = await _repository.GetUserAsync(User);

            return(user.Notifications.Select(x => new ApiNotification(x)));
        }
コード例 #5
0
        public async Task TestGetUser()
        {
            User user = await repository.GetUserAsync(1);

            Assert.Equal("user1", user.Username);
            Assert.Equal("*****@*****.**", user.Email);
        }
コード例 #6
0
        public async Task <IActionResult> Index(int page = 1)
        {
            // Retrieves the threads at the front page
            IEnumerable <Thread> threads = await _repository.GetFrontPageAsync(page);

            // Checks if the user's email is verified and sets EmailVerified accordingly
            bool emailVerified             = false;
            bool emailVerificationRequired = _config.RequireEmailVerification;

            if (User.Identity.IsAuthenticated)
            {
                User user = await _repository.GetUserAsync(User);

                emailVerified = user.Activated;
            }

            // Sets thread and page variables and returns view
            IndexViewModel model = new IndexViewModel()
            {
                Threads                   = threads,
                Page                      = page,
                PageCount                 = await _repository.GetPageCountAsync(),
                EmailVerified             = emailVerified,
                EmailVerificationRequired = emailVerificationRequired
            };

            return(View(model));
        }
コード例 #7
0
        // Returns a page of the requested thread
        public async Task <IActionResult> Index(int id, int page = 1)
        {
            // Retrieves the requested thread
            Thread thread = await _repository.GetThreadAsync(id);

            // Returns 404 if thread is null
            if (thread == null)
            {
                return(NotFound());
            }

            // Returns message of thread has been removed
            if (thread.Deleted || thread.User.Deleted)
            {
                MessageViewModel messageModel = new MessageViewModel()
                {
                    Title        = "Removed",
                    MessageTitle = "This thread has been removed"
                };
                return(View("Message", messageModel));
            }

            // Sets the current user
            User user = await _repository.GetUserAsync(User);

            // Retrieves comments for the requested page
            Result <IEnumerable <Comment> > result = _repository.GetThreadReplies(thread, page);

            if (result.Failure)
            {
                return(StatusCode(result.Code));
            }

            // Creates model and returns view
            ThreadViewModel model = new ThreadViewModel()
            {
                Thread    = thread,
                Comments  = result.Value,
                Page      = page,
                PageCount = ((thread.Comments.Count + 1) + (PostsPerPage - 1)) / PostsPerPage,
                User      = user
            };

            return(View("Thread", model));
        }
コード例 #8
0
        public async Task <IActionResult> GenerateTempToken()
        {
            User user = await _repository.GetUserAsync(User);

            TempApiToken token = await _repository.AddTempApiToken(user);

            await _repository.SaveChangesAsync();

            return(Ok(token.Token));
        }
コード例 #9
0
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            // Retrieves user and returns error if muted
            SimpleForum.Models.User user = await _repository.GetUserAsync(context.HttpContext.User);

            if (user.Muted)
            {
                context.Result = Forbid("Account muted, access denied");
                return;
            }

            await next();
        }
コード例 #10
0
        // Authenticates a user
        public async Task <Result <string> > Authenticate(string username, string password)
        {
            // Gets a user and returns failure if details are incorrect
            User user = await _repository.GetUserAsync(username);

            if (user == null)
            {
                return(Result.Fail <string>("username incorrect", 400));
            }

            if (!user.CheckPassword(password))
            {
                return(Result.Fail <string>("password incorrect", 400));
            }
            if (user.Deleted)
            {
                return(Result.Fail <string>("username incorrect", 400));
            }

            // Creates and returns a JWT token
            string token = JwtToken.CreateToken(username, user.UserID.ToString(), _config.PrivateKey);

            return(Result.Ok(token));
        }
コード例 #11
0
        public async Task InvokeAsync(HttpContext httpContext, SimpleForumRepository repository)
        {
            // Urls which can be accessed whilst an account is banned
            List <string> urlExceptions = new List <string>()
            {
                "/Error/Banned",
                "/Login/Logout",
                "/Error/StatusError"
            };

            List <bool> conditions = new List <bool>()
            {
                httpContext.User.Identity.IsAuthenticated,
                urlExceptions.All(x => x != httpContext.Request.Path.Value)
            };

            if (conditions.All(x => x))
            {
                User user = await repository.GetUserAsync(httpContext.User);

                if (user == null)
                {
                    await httpContext.SignOutAsync();
                }
                else if (user.Deleted)
                {
                    await httpContext.SignOutAsync();
                }
                else if (user.Banned)
                {
                    httpContext.Response.Redirect("/Error/Banned");
                }
            }

            await _next(httpContext);
        }
コード例 #12
0
        // Returns a user's profile
        public async Task <IActionResult> Index(int id, int page = 1)
        {
            // Retrieves users and return 404 if null
            User user = await _repository.GetUserAsync(id);

            if (user == null)
            {
                return(NotFound());
            }

            // Retrieves comments and logged in user
            IEnumerable <UserComment> userComments = _repository.GetUserComments(user, page);
            User currentUser = await _repository.GetUserAsync(User);

            // Creates model and returns view
            UserPageViewModel model = new UserPageViewModel()
            {
                User                = user,
                Page                = page,
                PageCount           = (user.UserPageComments.Count(x => !x.Deleted) + (CommentsPerPage - 1)) / CommentsPerPage,
                CurrentUser         = currentUser,
                CurrentPageComments = userComments
            };

            return(View("User", model));
        }