aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Services
diff options
context:
space:
mode:
Diffstat (limited to 'src/DevHive.Services')
-rw-r--r--src/DevHive.Services/Services/CommentService.cs17
-rw-r--r--src/DevHive.Services/Services/FeedService.cs8
-rw-r--r--src/DevHive.Services/Services/PostService.cs44
-rw-r--r--src/DevHive.Services/Services/UserService.cs37
4 files changed, 95 insertions, 11 deletions
diff --git a/src/DevHive.Services/Services/CommentService.cs b/src/DevHive.Services/Services/CommentService.cs
index e0eb88a..e2b54c4 100644
--- a/src/DevHive.Services/Services/CommentService.cs
+++ b/src/DevHive.Services/Services/CommentService.cs
@@ -12,7 +12,7 @@ using System.Linq;
namespace DevHive.Services.Services
{
- public class CommentService : ICommentService
+ public class CommentService : ICommentService
{
private readonly IUserRepository _userRepository;
private readonly IPostRepository _postRepository;
@@ -103,6 +103,9 @@ namespace DevHive.Services.Services
#endregion
#region Validations
+ /// <summary>
+ /// Checks whether the user Id in the token and the given user Id match
+ /// </summary>
public async Task<bool> ValidateJwtForCreating(Guid userId, string rawTokenData)
{
User user = await this.GetUserForValidation(rawTokenData);
@@ -110,6 +113,11 @@ namespace DevHive.Services.Services
return user.Id == userId;
}
+ /// <summary>
+ /// Checks whether the comment, gotten with the commentId,
+ /// is made by the user in the token
+ /// or if the user in the token is an admin
+ /// </summary>
public async Task<bool> ValidateJwtForComment(Guid commentId, string rawTokenData)
{
Comment comment = await this._commentRepository.GetByIdAsync(commentId) ??
@@ -126,6 +134,9 @@ namespace DevHive.Services.Services
return false;
}
+ /// <summary>
+ /// Returns the user, via their Id in the token
+ /// </summary>
private async Task<User> GetUserForValidation(string rawTokenData)
{
JwtSecurityToken jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7));
@@ -139,7 +150,9 @@ namespace DevHive.Services.Services
return user;
}
-
+ /// <summary>
+ /// Returns all values from a given claim type
+ /// </summary>
private List<string> GetClaimTypeValues(string type, IEnumerable<Claim> claims)
{
List<string> toReturn = new();
diff --git a/src/DevHive.Services/Services/FeedService.cs b/src/DevHive.Services/Services/FeedService.cs
index b9d1922..671df60 100644
--- a/src/DevHive.Services/Services/FeedService.cs
+++ b/src/DevHive.Services/Services/FeedService.cs
@@ -24,6 +24,10 @@ namespace DevHive.Services.Services
this._mapper = mapper;
}
+ /// <summary>
+ /// This method is used in the feed page.
+ /// See the FeedRepository "GetFriendsPosts" menthod for more information on how it works.
+ /// </summary>
public async Task<ReadPageServiceModel> GetPage(GetPageServiceModel model)
{
User user = null;
@@ -53,6 +57,10 @@ namespace DevHive.Services.Services
return readPageServiceModel;
}
+ /// <summary>
+ /// This method is used in the profile pages.
+ /// See the FeedRepository "GetUsersPosts" menthod for more information on how it works.
+ /// </summary>
public async Task<ReadPageServiceModel> GetUserPage(GetPageServiceModel model)
{
User user = null;
diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs
index 6dbb272..fe91f23 100644
--- a/src/DevHive.Services/Services/PostService.cs
+++ b/src/DevHive.Services/Services/PostService.cs
@@ -9,7 +9,7 @@ using System.Security.Claims;
using DevHive.Services.Interfaces;
using DevHive.Data.Interfaces.Repositories;
using System.Linq;
-using Microsoft.CodeAnalysis.CSharp;
+using DevHive.Data.RelationModels;
namespace DevHive.Services.Services
{
@@ -39,7 +39,10 @@ namespace DevHive.Services.Services
Post post = this._postMapper.Map<Post>(createPostServiceModel);
if (createPostServiceModel.Files.Count != 0)
- post.FileUrls = await _cloudService.UploadFilesToCloud(createPostServiceModel.Files);
+ {
+ List<string> fileUrls = await _cloudService.UploadFilesToCloud(createPostServiceModel.Files);
+ post.Attachments = this.GetPostAttachmentsFromUrls(post, fileUrls);
+ }
post.Creator = await this._userRepository.GetByIdAsync(createPostServiceModel.CreatorId);
post.TimeCreated = DateTime.Now;
@@ -77,6 +80,7 @@ namespace DevHive.Services.Services
readPostServiceModel.CreatorFirstName = user.FirstName;
readPostServiceModel.CreatorLastName = user.LastName;
readPostServiceModel.CreatorUsername = user.UserName;
+ readPostServiceModel.FileUrls = post.Attachments.Select(x => x.FileUrl).ToList();
return readPostServiceModel;
}
@@ -94,14 +98,15 @@ namespace DevHive.Services.Services
{
if (await this._postRepository.DoesPostHaveFiles(updatePostServiceModel.PostId))
{
- List<string> fileUrls = await this._postRepository.GetFileUrls(updatePostServiceModel.PostId);
- bool success = await _cloudService.RemoveFilesFromCloud(fileUrls);
+ List<string> fileUrlsToRemove = await this._postRepository.GetFileUrls(updatePostServiceModel.PostId);
+ bool success = await _cloudService.RemoveFilesFromCloud(fileUrlsToRemove);
if (!success)
throw new InvalidCastException("Could not delete files from the post!");
}
- post.FileUrls = await _cloudService.UploadFilesToCloud(updatePostServiceModel.Files) ??
+ List<string> fileUrls = await _cloudService.UploadFilesToCloud(updatePostServiceModel.Files) ??
throw new ArgumentNullException("Unable to upload images to cloud");
+ post.Attachments = this.GetPostAttachmentsFromUrls(post, fileUrls);
}
post.Creator = await this._userRepository.GetByIdAsync(updatePostServiceModel.CreatorId);
@@ -138,6 +143,9 @@ namespace DevHive.Services.Services
#endregion
#region Validations
+ /// <summary>
+ /// Checks whether the user Id in the token and the given user Id match
+ /// </summary>
public async Task<bool> ValidateJwtForCreating(Guid userId, string rawTokenData)
{
User user = await this.GetUserForValidation(rawTokenData);
@@ -145,6 +153,11 @@ namespace DevHive.Services.Services
return user.Id == userId;
}
+ /// <summary>
+ /// Checks whether the post, gotten with the postId,
+ /// is made by the user in the token
+ /// or if the user in the token is an admin
+ /// </summary>
public async Task<bool> ValidateJwtForPost(Guid postId, string rawTokenData)
{
Post post = await this._postRepository.GetByIdAsync(postId) ??
@@ -161,6 +174,11 @@ namespace DevHive.Services.Services
return false;
}
+ /// <summary>
+ /// Checks whether the comment, gotten with the commentId,
+ /// is made by the user in the token
+ /// or if the user in the token is an admin
+ /// </summary>
public async Task<bool> ValidateJwtForComment(Guid commentId, string rawTokenData)
{
Comment comment = await this._commentRepository.GetByIdAsync(commentId) ??
@@ -177,6 +195,9 @@ namespace DevHive.Services.Services
return false;
}
+ /// <summary>
+ /// Returns the user, via their Id in the token
+ /// </summary>
private async Task<User> GetUserForValidation(string rawTokenData)
{
JwtSecurityToken jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7));
@@ -190,6 +211,9 @@ namespace DevHive.Services.Services
return user;
}
+ /// <summary>
+ /// Returns all values from a given claim type
+ /// </summary>
private List<string> GetClaimTypeValues(string type, IEnumerable<Claim> claims)
{
List<string> toReturn = new();
@@ -201,5 +225,15 @@ namespace DevHive.Services.Services
return toReturn;
}
#endregion
+
+ #region Misc
+ private List<PostAttachments> GetPostAttachmentsFromUrls(Post post, List<string> fileUrls)
+ {
+ List<PostAttachments> postAttachments = new List<PostAttachments>();
+ foreach (string url in fileUrls)
+ postAttachments.Add(new PostAttachments { Post = post, FileUrl = url });
+ return postAttachments;
+ }
+ #endregion
}
}
diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs
index b3a4987..9cc4a8e 100644
--- a/src/DevHive.Services/Services/UserService.cs
+++ b/src/DevHive.Services/Services/UserService.cs
@@ -47,6 +47,10 @@ namespace DevHive.Services.Services
}
#region Authentication
+ /// <summary>
+ /// Adds a new user to the database with the values from the given model.
+ /// Returns a JSON Web Token (that can be used for authorization)
+ /// </summary>
public async Task<TokenModel> LoginUser(LoginServiceModel loginModel)
{
if (!await this._userRepository.DoesUsernameExistAsync(loginModel.UserName))
@@ -60,6 +64,9 @@ namespace DevHive.Services.Services
return new TokenModel(WriteJWTSecurityToken(user.Id, user.UserName, user.Roles));
}
+ /// <summary>
+ /// Returns a new JSON Web Token (that can be used for authorization) for the given user
+ /// </summary>
public async Task<TokenModel> RegisterUser(RegisterServiceModel registerModel)
{
if (await this._userRepository.DoesUsernameExistAsync(registerModel.UserName))
@@ -70,7 +77,7 @@ namespace DevHive.Services.Services
User user = this._userMapper.Map<User>(registerModel);
user.PasswordHash = PasswordModifications.GeneratePasswordHash(registerModel.Password);
- user.ProfilePicture = new ProfilePicture() { PictureURL = String.Empty };
+ user.ProfilePicture = new ProfilePicture() { PictureURL = "/assets/images/feed/profile-pic.png" };
// Make sure the default role exists
//TODO: Move when project starts
@@ -125,6 +132,9 @@ namespace DevHive.Services.Services
return this._userMapper.Map<UserServiceModel>(newUser);
}
+ /// <summary>
+ /// Uploads the given picture and assigns it's link to the user in the database
+ /// </summary>
public async Task<ProfilePictureServiceModel> UpdateProfilePicture(UpdateProfilePictureServiceModel updateProfilePictureServiceModel)
{
User user = await this._userRepository.GetByIdAsync(updateProfilePictureServiceModel.UserId);
@@ -162,6 +172,11 @@ namespace DevHive.Services.Services
#endregion
#region Validations
+ /// <summary>
+ /// Checks whether the given user, gotten by the "id" property,
+ /// is the same user as the one in the token (uness the user in the token has the admin role)
+ /// and the roles in the token are the same as those in the user, gotten by the id in the token
+ /// </summary>
public async Task<bool> ValidJWT(Guid id, string rawTokenData)
{
// There is authorization name in the beginning, i.e. "Bearer eyJh..."
@@ -176,9 +191,6 @@ namespace DevHive.Services.Services
/* Check if user is trying to do something to himself, unless he's an admin */
/* Check roles */
- if (jwtRoleNames.Contains(Role.AdminRole))
- return true;
-
if (!jwtRoleNames.Contains(Role.AdminRole))
if (user.Id != id)
return false;
@@ -197,6 +209,9 @@ namespace DevHive.Services.Services
return true;
}
+ /// <summary>
+ /// Returns all values from a given claim type
+ /// </summary>
private List<string> GetClaimTypeValues(string type, IEnumerable<Claim> claims)
{
List<string> toReturn = new();
@@ -208,6 +223,11 @@ namespace DevHive.Services.Services
return toReturn;
}
+ /// <summary>
+ /// Checks whether the user in the model exists
+ /// and whether the username in the model is already taken.
+ /// If the check fails (is false), it throws an exception, otherwise nothing happens
+ /// </summary>
private async Task ValidateUserOnUpdate(UpdateUserServiceModel updateUserServiceModel)
{
if (!await this._userRepository.DoesUserExistAsync(updateUserServiceModel.Id))
@@ -218,6 +238,10 @@ namespace DevHive.Services.Services
throw new ArgumentException("Username already exists!");
}
+ /// <summary>
+ /// Return a new JSON Web Token, containing the user id, username and roles.
+ /// Tokens have an expiration time of 7 days.
+ /// </summary>
private string WriteJWTSecurityToken(Guid userId, string username, HashSet<Role> roles)
{
byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret);
@@ -274,6 +298,11 @@ namespace DevHive.Services.Services
return new TokenModel(WriteJWTSecurityToken(newUser.Id, newUser.UserName, newUser.Roles));
}
+ /// <summary>
+ /// Returns the user with the Id in the model, adding to him the roles, languages and technologies, specified by the parameter model.
+ /// This practically maps HashSet<UpdateRoleServiceModel> to HashSet<Role> (and the equvalent HashSets for Languages and Technologies)
+ /// and assigns the latter to the returned user.
+ /// </summary>
private async Task<User> PopulateModel(UpdateUserServiceModel updateUserServiceModel)
{
User user = this._userMapper.Map<User>(updateUserServiceModel);