Example #1
0
        [HttpGet("friends/get")] // DONE
        public async Task <IActionResult> GetAllFriends()
        {
            User user = await UserManager.GetUserAsync(base.User);

            // List of friends for users
            List <string> friends = new List <string>();

            friends.AddRange((from relation in Database.UserRelations
                              where relation.FromUser == user
                              select relation.ToUserId)
                             .ToArray());

            friends.AddRange((from relation in Database.UserRelations
                              where relation.ToUser == user
                              select relation.FromUserId)
                             .ToArray());

            // If there aren't any available friends, return bad request
            if (friends.Count != 0)
            {
                return(BadRequest($"User '{user.UserName}' has no friends added"));
            }

            // Will not sort the list here, better to sort it on client end

            return(Content(friends.ToJson()));
        }
Example #2
0
        [HttpPost("update")] // Update all information except username and password
        public async Task <IActionResult> UpdateUserDetails([FromBody] CloudBook.Data.User form)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Error 1000: Bad data provided"));
            }

            // Get Identity information of User
            User user = await UserManager.GetUserAsync(base.User);

            // Update user information
            user.Update(ref form);

            // Sync with database
            var result = await UserManager.UpdateAsync(user);

            if (!result.Succeeded)
            {
                // TODO: Add Error Log to file
                return(BadRequest("There was an error while updating user information"));
            }

            // Create a new security stamp. Stamps are updated each time a user makes changes
            await UserManager.UpdateSecurityStampAsync(user);

            return(Ok());
        }
Example #3
0
        public async Task <IActionResult> VerifyEmail(string usr, string token)
        {
            try
            {
                User user = await UserManager.FindByNameAsync(usr);

                // Create a new security stamp. Stamps are updated each time a user makes changes
                await UserManager.UpdateSecurityStampAsync(user);

                // If user's email is already confirmed, return bad req
                if (user.EmailConfirmed)
                {
                    return(BadRequest($"User {user.UserName} is already verified"));
                }

                // Provide token and user data to confirm email.
                var result = await UserManager.ConfirmEmailAsync(user, token);

                if (result.Succeeded)
                {
                    return(Ok($"User {user.UserName} has been varified"));
                }
                else
                {
                    return(BadRequest(result.Errors.ToJson()));
                }
            }
            catch (Exception ex) { return(BadRequest(ex.Message)); }
        }
Example #4
0
        [HttpPost("friends/add")] // UNTESTED
        public async Task <IActionResult> AddFriend([FromBody] string username)
        {
            User user = await UserManager.GetUserAsync(User);

            User friend = await UserManager.FindByNameAsync(username);

            if (friend.Equals(null))
            {
                return(BadRequest($"{username} doesn't exist"));
            }

            // Find if there is already a relation established between users
            bool isFriend = (from relation in Database.UserRelations
                             where (relation.FromUserId == user.UserName && relation.ToUserId == username) ||
                             (relation.ToUserId == user.UserName && relation.FromUserId == username)
                             select relation)
                            .Any();

            if (isFriend)
            {
                return(BadRequest("Users are already friends"));
            }

            // Find if there are any friends request between the users
            isFriend = (from request in Database.Requests
                        where (request.UserName == user.UserName && request.Target == friend.UserName) ||
                        (request.UserName == friend.UserName && request.Target == user.UserName)
                        select request).Any();

            if (isFriend)
            {
                return(BadRequest($"A friend request between {user.UserName} and {friend.UserName} already exists"));
            }

            Database.Requests.Add(new Request()
            {
                Target = friend.UserName, User = user
            });

            // Add a new relation between the two users, with time of creation being NOW
            Database.UserRelations.Add(new UserRelation()
            {
                FromUser = user,
                ToUser   = friend,
                Date     = DateTime.UtcNow
            });

            await Database.SaveChangesAsync();

            return(Ok());
        }
Example #5
0
        public async Task <IActionResult> Regiser([FromBody] Registration form)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Error 1000: Bad data provided"));
            }

            var user = new User(ref form);

            // Generate Hashsed password
            user.PasswordHash = UserManager.PasswordHasher.HashPassword(user, form.Password);

            var result = await UserManager.CreateAsync(user);

            if (!result.Succeeded)
            {
                return(BadRequest(error: result.Errors.ToList().ToString()));
            }

            // Update security stamp. Update it everytime something happens with an user
            await UserManager.UpdateSecurityStampAsync(user);

            // Get token for confirming email. Without token, email cannot be confirmed
            string token_link = await UserManager.GenerateEmailConfirmationTokenAsync(user);

            // Make the token uri-friendly
            token_link = Uri.EscapeUriString(token_link);

            // Create absolute link to varify the email providing username and token
            token_link = $"http://207.148.16.163/api/v1/user/varify?usr={user.UserName}&token={token_link}";

            // Doesn't work yet. Keep it commented before building/running
            Task task = SendVerificationEmailAsync(user.Email, token_link);

            // Run it with timeout limit
            await task.ConfigureAwait(false);

            if (task.IsCompletedSuccessfully)
            {
                return(Ok());
            }
            else
            {
                return(BadRequest("Failed to send the email"));
            }
        }
Example #6
0
        public async Task <IActionResult> ChangeUserName(string user_name)
        {
            // Get current User's Identity
            User user_identity = await UserManager.GetUserAsync(User);

            // Attempt to update the user name of current User's identity
            var result = await UserManager.SetUserNameAsync(user_identity, user_name);

            if (!result.Succeeded)
            {
                return(BadRequest($"Error: {result.Errors}"));
            }

            // Create a new security stamp. Stamps are updated each time a user makes changes
            await UserManager.UpdateSecurityStampAsync(user_identity);

            return(Accepted());
        }