示例#1
0
        public async Task <GenericResponseDTO <int> > AddTag(CreateTagDTO newTag)
        {
            var currentUserId = authHelper.GetCurrentUserId(User);

            // Only allow the teacher to tag a project
            var project = await database.Projects
                          .AsQueryable()
                          .FirstAsync(x => x.Id == newTag.ProjectId && x.Teacher.Id == currentUserId && x.ArchivedDate == null);

            if (project == null)
            {
                return(new GenericResponseDTO <int>()
                {
                    Message = "Couldn't find the project",
                    Success = false
                });
            }

            var tag = new Tag()
            {
                Name    = newTag.Tag,
                Project = project
            };

            await database.AddAsync(tag);

            await database.SaveChangesAsync();

            return(new GenericResponseDTO <int>()
            {
                Data = tag.Id,
                Success = true
            });
        }
示例#2
0
        public async Task <GenericResponseDTO <AccessKeysDTO> > Login(UserDTO loginData)
        {
            try
            {
                // Get user with a matching username and password hash
                var    hashedPassword = authHelper.GetPasswordHash(loginData.Password, configuration);
                String message        = "";
                var    curUser        = await database.Users
                                        .Include(x => x.Projects)
                                        .FirstOrDefaultAsync(u => u.Email.ToLower() == loginData.Email.ToLower() && u.Password.SequenceEqual(hashedPassword));

                // If there was not a matching user then return an error
                if (curUser == null)
                {
                    return(new GenericResponseDTO <AccessKeysDTO>()
                    {
                        Success = false,
                        Message = "Invalid username or password"
                    });
                }

                // Generate the JWT and Refresh tokens and save the refresh token to the db
                var accessToken  = authHelper.GenerateJSONWebToken(curUser, configuration);
                var refreshToken = authHelper.GenerateRefreshToken();

                var userRefreshToken = new RefreshToken()
                {
                    Token = refreshToken,
                    User  = curUser
                };

                // check if the user logged in with an invite code, if they did, add them to a project
                if (!String.IsNullOrWhiteSpace(loginData.InviteCode))
                {
                    Project project = await database.Projects
                                      .AsQueryable()
                                      .FirstOrDefaultAsync(p => p.InviteCode == loginData.InviteCode);

                    if (project != null)
                    {
                        if (project.ArchivedDate != null)
                        {
                            message = "Unable to add to Archived Project";
                        }
                        else
                        {
                            if (curUser.Projects.Contains(project))
                            {
                                message = "User already in project";
                            }
                            else
                            {
                                if (project.Teacher == curUser)
                                {
                                    message = "User already in project";
                                }
                                else
                                {
                                    curUser.Projects.Add(project);
                                    message = "Added User to Project";
                                }
                            }
                        }
                    }
                    else
                    {
                        message = "Project not found";
                    }
                }

                await database.AddAsync(userRefreshToken);

                await database.SaveChangesAsync();

                return(new GenericResponseDTO <AccessKeysDTO>()
                {
                    Success = true,
                    Data = new AccessKeysDTO()
                    {
                        AccessToken = accessToken,
                        RefreshToken = refreshToken
                    },
                    Message = message
                });
            }
            catch
            {
                return(new GenericResponseDTO <AccessKeysDTO>()
                {
                    Success = false,
                    Message = "An unknown error has occurred"
                });
            }
        }