public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null) { return(new StatusCodeResult(403)); } string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); SearchModel data = JsonConvert.DeserializeObject <SearchModel>(requestBody); ISearchIndexClient indexClient = new SearchIndexClient(Environment.GetEnvironmentVariable("SearchServiceName"), "azuresql-index", new SearchCredentials(Environment.GetEnvironmentVariable("SearchServiceQueryApiKey"))); using (DataContext dc = new DataContext()) { var sp = new SearchParameters(); if (!string.IsNullOrEmpty(data.Filter)) { sp.Filter = data.Filter; } DocumentSearchResult <User> reponses = indexClient.Documents.Search <User>(data.SearchString, sp); return(new JsonResult(reponses)); } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null) { return(new StatusCodeResult(403)); } string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); DownloadDocumentModel data; try { data = JsonConvert.DeserializeObject <DownloadDocumentModel>(requestBody); } catch { return(new BadRequestResult()); } using (DataContext dc = new DataContext()) { Document d = dc.Documents.Where(x => x.User.UserID == requester.UserID && x.FileName == data.FileName).FirstOrDefault(); if (d != null) { dc.Documents.Remove(d); } dc.SaveChanges(); } return(new OkResult()); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null || (requester.UserType.UserTypeName != "Admin" && requester.UserType.UserTypeName != "Manager")) { return(new StatusCodeResult(403)); } List <string> errors = new List <string>(); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); TeamIDModel teamID; try { teamID = JsonConvert.DeserializeObject <TeamIDModel>(requestBody); } catch { return(new BadRequestResult()); } using (DataContext dc = new DataContext()) { Team team = dc.Team.Include(x => x.Users).Where(x => x.TeamID == teamID.TeamID).FirstOrDefault(); if (team == null) { errors.Add("Team does not exist"); } if (errors.Count > 0) { return(new BadRequestObjectResult(errors)); } foreach (User u in team.Users) { if (u.UserID != team.LeaderID) { dc.User.Where(x => x.UserID == u.UserID).Single().Team = null; } } team.Users = new List <User> { dc.User.Where(x => x.UserID == team.LeaderID).Single() }; dc.SaveChanges(); return(new OkResult()); } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null || requester.UserType.UserTypeName != "Admin") { return(new StatusCodeResult(403)); } List <string> errors = new List <string>(); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); Role role; try { role = JsonConvert.DeserializeObject <Role>(requestBody); } catch { return(new BadRequestResult()); } if (string.IsNullOrWhiteSpace(requestBody) || role == null) { return(new BadRequestResult()); } using (DataContext dc = new DataContext()) { Role r = dc.Roles.Where(x => x.RoleID == role.RoleID).FirstOrDefault(); if (r == null) { errors.Add("Role does not exist"); } User u = dc.User.Where(x => x.RoleID == role.RoleID).FirstOrDefault(); if (u != null) { errors.Add("There are currently users with that role. Make sure this role is not used before deleting"); } if (errors.Count > 0) { return(new BadRequestObjectResult(errors)); } dc.Remove(r); dc.SaveChanges(); } return(new OkResult()); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null) { return(new StatusCodeResult(403)); } List <string> errors = new List <string>(); UserIDModel data = new UserIDModel(); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); try { data = JsonConvert.DeserializeObject <UserIDModel>(requestBody); } catch { return(new BadRequestResult()); } using (DataContext dc = new DataContext()) { User u = dc.User.Where(x => x.UserID == data.UserID).FirstOrDefault(); if (u == null) { errors.Add("User does not exist"); } if (errors.Count > 0) { return(new BadRequestObjectResult(errors)); } Session s = dc.Sessions.Include(x => x.User).Where(x => x.User.UserID == u.UserID).FirstOrDefault(); if (s == null) { return(new BadRequestResult()); } dc.Remove(s); dc.SaveChanges(); } return(new OkResult()); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null) { return(new StatusCodeResult(403)); } string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); DownloadDocumentModel data; Document d; try { data = JsonConvert.DeserializeObject <DownloadDocumentModel>(requestBody); } catch { return(new BadRequestResult()); } using (DataContext dc = new DataContext()) { User u = dc.User.Include(x => x.Documents).Where(x => x.UserID == data.UserID).FirstOrDefault(); if (u == null) { return(new BadRequestObjectResult("User ID does not exist")); } if (requester.UserType.UserTypeName != "Admin" && u.UserID != requester.UserID) { return(new StatusCodeResult(403)); } d = u.Documents.Where(x => x.User.UserID == u.UserID && x.FileName == data.FileName).FirstOrDefault(); if (d == null) { return(new BadRequestObjectResult("File does not exist")); } } return(new JsonResult(d.FileLocation)); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null) { return(new StatusCodeResult(403)); } IEnumerable <Notification> notifications = new List <Notification>(); using (DataContext dc = new DataContext()) { notifications = dc.Notification.Where(x => x.User == requester).ToList(); } return(new JsonResult(notifications)); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null) { return(new StatusCodeResult(403)); } string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); NotificationModel data; try { data = JsonConvert.DeserializeObject <NotificationModel>(requestBody); } catch { return(new BadRequestResult()); } if (string.IsNullOrWhiteSpace(requestBody) || data == null) { return(new BadRequestResult()); } using (DataContext dc = new DataContext()) { User u = dc.User.Where(x => x.UserID == requester.UserID).FirstOrDefault(); if (u != null) { u.PhoneToken = data.PhoneToken; } dc.SaveChanges(); } return(new OkResult()); }
public static IActionResult Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null) { return(new StatusCodeResult(403)); } List <CreateUserModel> cums = new List <CreateUserModel>(); using (DataContext dc = new DataContext()) { foreach (var User in dc.User.Include(x => x.Team).Include(x => x.Role).Include(x => x.Documents).Include(x => x.UserSkills).ThenInclude(y => y.Skill)) { string TeamID = null; if (User.Team != null) { TeamID = User.Team.TeamID.ToString(); } CreateUserModel cum = new CreateUserModel { Name = User.Name, Role = User.Role.Title, Skills = User.UserSkills.Select(x => x.Skill.SkillName).ToList(), TeamID = TeamID, UserID = User.UserID }; Document d = User.Documents.Where(x => x.IsProfilePicture).SingleOrDefault(); if (d != null) { var webClient = new WebClient(); byte[] f = webClient.DownloadData(d.FileLocation); cum.ProfilePicture = f; } cums.Add(cum); } } return(new JsonResult(cums)); }
public static IActionResult Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null) { return new StatusCodeResult(403); } List<CreateTeamModel> ctms = new List<CreateTeamModel>(); using (DataContext dc = new DataContext()) { var teams = dc.Team.Include(x => x.Users).Where(x => x.Users.Contains(requester) || requester.UserType.UserTypeName == "Admin"); foreach (var Team in teams) { CreateTeamModel ctm = new CreateTeamModel { TeamName = Team.TeamName, LeaderID = Team.LeaderID, ProjectName = Team.ProjectName, TeamID = Team.TeamID}; ctms.Add(ctm); } } return new JsonResult(ctms); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null || requester.UserType.UserTypeName != "Admin") { return(new StatusCodeResult(403)); } List <string> errors = new List <string>(); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); TeamIDModel data; Team team; try { data = JsonConvert.DeserializeObject <TeamIDModel>(requestBody); } catch { return(new BadRequestResult()); } if (string.IsNullOrWhiteSpace(requestBody) || data == null) { return(new BadRequestResult()); } using (DataContext dc = new DataContext()) { Team t = dc.Team.Where(x => x.TeamID == data.TeamID).FirstOrDefault(); if (t == null) { errors.Add("Team does not exist"); } if (errors.Count > 0) { return(new BadRequestObjectResult(errors)); } //Creates a list of users who are in the team we want to delete and removes them each from the team before it is deleted IEnumerable <User> UsersInTeam = dc.User.Where(x => x.Team.TeamID == t.TeamID); foreach (User u in UsersInTeam) { NotificationHandler.SendNotification(new Notification { Title = "Team Update", Body = "You have been removed from your team", User = u }, dc); u.Team = null; } if (t != null) { User leader = dc.User.Where(x => x.UserID == t.LeaderID).FirstOrDefault(); if (leader != null) { UserType type = dc.UserTypes.Where(x => x.UserTypeName == "User").FirstOrDefault(); leader.UserType = type; //Changes usertype to basic when manager is removed from team } } team = t; dc.Remove(team); dc.SaveChanges(); } return(new OkResult()); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null || requester.UserType.UserTypeName != "Admin") { return(new StatusCodeResult(403)); } List <string> errors = new List <string>(); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); CreateUserModel data; try { data = JsonConvert.DeserializeObject <CreateUserModel>(requestBody); } catch { return(new BadRequestResult()); } if (string.IsNullOrWhiteSpace(requestBody) || data == null) { return(new BadRequestResult()); } using (DataContext dc = new DataContext()) { if (data.Mobile == null || data.Mobile.Length > 13) { errors.Add("Mobile number must be less than 13 digits."); } if (string.IsNullOrWhiteSpace(data.Email)) { errors.Add("E-mail cannot be empty"); } if (string.IsNullOrWhiteSpace(data.Name)) { errors.Add("Name cannot be empty"); } UserType u = dc.UserTypes.Where(x => x.UserTypeName == data.UserType).FirstOrDefault(); if (u == null) { errors.Add("User type does not exist"); } if (string.IsNullOrWhiteSpace(data.Role)) { errors.Add("User must have a role"); } if (errors.Count > 0) { return(new BadRequestObjectResult(errors)); } // checking for existing element; create if it doesn't exist Role r = dc.Roles.Where(x => x.Title == data.Role).FirstOrDefault(); if (r == null) { r = new Role { Title = data.Role }; dc.Roles.Add(r); } MaritalStatus m = dc.MaritalStatuses.Where(x => x.MaritalStatusName == data.MaritalStatus).FirstOrDefault(); if (m == null) { if (data.MaritalStatus != null) { m = new MaritalStatus { MaritalStatusName = data.MaritalStatus }; dc.MaritalStatuses.Add(m); } } User user = new User { Name = data.Name, Mobile = data.Mobile, DateOfBirth = data.DateOfBirth, Role = r, UserType = u, Address = data.Address, Email = data.Email, NextOfKin1 = data.NextOfKin1, NextOfKin2 = data.NextOfKin2, MaritalStatus = m, Nationality = data.Nationality, VisaStatus = data.VisaStatus, Gender = data.Gender, MedicalStatus = data.MedicalStatus, DateTimeUpdated = DateTime.Now }; dc.User.Add(user); if (data.Languages != null) { foreach (string language in data.Languages) { Language l = dc.Languages.Where(x => x.LanguageName == language).FirstOrDefault(); if (l == null) { l = new Language { LanguageName = language }; dc.Languages.Add(l); } dc.UserLanguages.Add(new UserLanguage { Language = l, User = user }); } } if (data.Skills != null) { foreach (string skill in data.Skills) { Skill s = dc.Skills.Where(x => x.SkillName == skill).FirstOrDefault(); if (s == null) { s = new Skill { SkillName = skill }; dc.Skills.Add(s); } dc.UserSkills.Add(new UserSkill { Skill = s, User = user }); } } string salt = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); string hashedPassword = SessionValidator.ComputeHash(data.Password, salt); Authentication login = new Authentication { PasswordHash = hashedPassword, User = user, Salt = salt }; dc.Authentication.Add(login); dc.SaveChanges(); return(new OkResult()); } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null || requester.UserType.UserTypeName != "Admin") { return(new StatusCodeResult(403)); } List <string> errors = new List <string>(); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); TeamIDModel teamID; try { teamID = JsonConvert.DeserializeObject <TeamIDModel>(requestBody); } catch { return(new BadRequestResult()); } using (DataContext dc = new DataContext()) { //Links to the database User user = dc.User.Include(x => x.UserType).Where(x => x.Name == teamID.userName).FirstOrDefault(); if (user == null) { errors.Add("User does not exist"); } Team team = dc.Team.Include(x => x.Users).Where(x => x.TeamID == teamID.TeamID).FirstOrDefault(); if (team == null) { errors.Add("Team does not exist"); } if (errors.Count > 0) { return(new BadRequestObjectResult(errors)); } // Demote previous leader User previousLeader = dc.User.Include(x => x.UserType).Where(x => x.UserID == team.LeaderID).FirstOrDefault(); if (previousLeader != null) { if (previousLeader.UserType.UserTypeName != "Admin") { previousLeader.UserType = dc.UserTypes.Where(x => x.UserTypeName == "User").FirstOrDefault(); } NotificationHandler.SendNotification(new Notification { User = previousLeader, Title = "Team Update", Body = "You were removed as team leader from " + team.TeamName }, dc); } if (user.Team != team) { user.Team = team; } UserType t = dc.UserTypes.Where(x => x.UserTypeName == "Manager").FirstOrDefault(); if (user.UserType.UserTypeName != "Admin") { user.UserType = t; //Changes usertype to manager when made leader of a team } team.LeaderID = user.UserID; NotificationHandler.SendNotification(new Notification { User = user, Title = "Team Update", Body = "You were made team leader of " + team.TeamName }, dc); foreach (User u in team.Users) { if (u != user && u != previousLeader) { NotificationHandler.SendNotification(new Notification { User = u, Title = "Team Update", Body = user.Name + " has been made your team leader" }, dc); } } dc.SaveChanges(); return(new OkResult()); } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null) { return(new StatusCodeResult(403)); } UserIDModel data = new UserIDModel(); bool isProfilePicture; try { req.Form.TryGetValue("id", out StringValues id); req.Form.TryGetValue("isProfilePicture", out StringValues ProfilePicture); bool.TryParse(ProfilePicture, out isProfilePicture); data.UserID = Convert.ToInt32(id.ToString()); } catch { return(new BadRequestResult()); } using (DataContext dc = new DataContext()) { User u = dc.User.Where(x => x.UserID == data.UserID).FirstOrDefault(); if (u == null) { return(new BadRequestResult()); } if (requester.UserType.UserTypeName != "Admin" && requester.UserID != u.UserID) { return(new StatusCodeResult(403)); } var f = req.Form.Files[0]; // Create Reference to Azure Storage Account string strorageconn = "DefaultEndpointsProtocol=https;AccountName=team10projecta916;AccountKey=4V9RYDcZUjn442xOgaSW/RwC7mzB+zguDgfHM0tyqff8cSeomPMj6cvOA2ATav0Vor2eoAR64A7L+DCsFDrZCw==;EndpointSuffix=core.windows.net"; CloudStorageAccount storageacc = CloudStorageAccount.Parse(strorageconn); //Create Reference to Azure Blob CloudBlobClient blobClient = storageacc.CreateCloudBlobClient(); //The next 2 lines create if not exists a container named "democontainer" CloudBlobContainer container = blobClient.GetContainerReference("user" + data.UserID.ToString()); await container.CreateIfNotExistsAsync(); await container.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }); //The next 7 lines upload the file CloudBlockBlob blockBlob = container.GetBlockBlobReference(f.FileName); using (var filestream = f.OpenReadStream()) { await blockBlob.UploadFromStreamAsync(filestream); } if (isProfilePicture) { Document doc = dc.Documents.Where(x => x.IsProfilePicture && x.User == u).FirstOrDefault(); if (doc != null) { dc.Documents.Remove(doc); } } Document d = dc.Documents.Where(x => x.FileName == f.FileName).FirstOrDefault(); if (d != null) { dc.Documents.Remove(d); } dc.Documents.Add(new Document { User = u, FileName = f.FileName, FileLocation = blockBlob.Uri.AbsoluteUri, IsProfilePicture = isProfilePicture }); dc.SaveChanges(); } return(new OkResult()); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null || (requester.UserType.UserTypeName != "Admin" && requester.UserType.UserTypeName != "Manager")) { return(new StatusCodeResult(403)); } List <string> errors = new List <string>(); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); CreateTeamModel data; try { data = JsonConvert.DeserializeObject <CreateTeamModel>(requestBody); } catch { return(new BadRequestResult()); } using (DataContext dc = new DataContext()) { List <Notification> n = new List <Notification>(); //Links to the database if (data.TeamID == null) { return(new BadRequestResult()); } Team original = dc.Team.Include(x => x.Users).Where(x => x.TeamID == data.TeamID).FirstOrDefault(); if (original == null) { errors.Add("Team cannot be found"); return(new BadRequestObjectResult(errors)); } //Team Name Statement if (!string.IsNullOrWhiteSpace(data.TeamName)) { Team t = dc.Team.Where(x => x.TeamName == data.TeamName).FirstOrDefault(); if (t != null) { errors.Add("Team with that name already exists"); } else { n.Add(new Notification { Title = "Team Update", Body = "Your team name has been changed to " + data.TeamName }); original.TeamName = data.TeamName; } } //Project Name if (!string.IsNullOrWhiteSpace(data.ProjectName)) { original.ProjectName = data.ProjectName; n.Add(new Notification { Title = "Team Update", Body = "Your team's project has been changed to " + data.ProjectName }); } if (errors.Count > 0) { return(new BadRequestObjectResult(errors)); } foreach (User u in original.Users) { if (n.Count == 1) { Notification no = n.First(); NotificationHandler.SendNotification(new Notification { Title = no.Title, Body = no.Body, User = u }, dc); } else if (n.Count > 1) { NotificationHandler.SendNotification(new Notification { Title = "Team Update", Body = "Your team information has been updated", User = u }, dc); } } //Saves the changes. dc.SaveChanges(); } return(new JsonResult(data)); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null) { return(new StatusCodeResult(403)); } List <string> errors = new List <string>(); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); ChangePasswordModel data; try { data = JsonConvert.DeserializeObject <ChangePasswordModel>(requestBody); } catch { return(new BadRequestResult()); } using (DataContext dc = new DataContext()) { //Check if email or password entered is null if (data.OldPassword == null) { return(new BadRequestResult()); } if (data.NewPassword == null) { return(new BadRequestResult()); } Authentication original = dc.Authentication.Where(x => x.User.UserID == requester.UserID).FirstOrDefault(); //If no entry found, return error if (original == null) { errors.Add("Username or Password is incorrect"); return(new BadRequestObjectResult(errors)); } string hashedOldPassword = SessionValidator.ComputeHash(data.OldPassword, original.Salt); //If password is incorrect, return error if (!hashedOldPassword.Equals(original.PasswordHash)) { errors.Add("Username or Password is incorrect"); return(new BadRequestObjectResult(errors)); } original.PasswordHash = SessionValidator.ComputeHash(data.NewPassword, original.Salt); dc.SaveChanges(); return(new OkResult()); } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null) { return(new StatusCodeResult(403)); } string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); CreateUserModel data; try { data = JsonConvert.DeserializeObject <CreateUserModel>(requestBody); } catch { return(new BadRequestResult()); } using (DataContext dc = new DataContext()) { List <Notification> n = new List <Notification>(); //Links to the database User original = dc.User.Include(x => x.MaritalStatus).Include(x => x.Role).Where(x => x.UserID == data.UserID).FirstOrDefault(); if (original == null) { return(new BadRequestResult()); } if (requester.UserType.UserTypeName != "Admin" && requester.UserID != original.UserID) { return(new StatusCodeResult(403)); } //Each if statement checks if a new value has been inserted (IE, not null) then pushes for the change to be made. //Name Statement if (string.IsNullOrWhiteSpace(data.Name) == false && original.Name != data.Name) { original.Name = data.Name; n.Add(new Notification { User = original, Title = "Profile Update", Body = "Your name has been changed to " + data.Name.ToString() }); } //Mobile If statement if (data.Mobile != null && data.Mobile.Length < 13 && original.Mobile != data.Mobile) { original.Mobile = data.Mobile; n.Add(new Notification { User = original, Title = "Profile Update", Body = "Your mobile number has been changed to " + data.Mobile.ToString() }); } //DoB if (data.DateOfBirth != null && original.DateOfBirth != data.DateOfBirth) { original.DateOfBirth = data.DateOfBirth; n.Add(new Notification { User = original, Title = "Profile Update", Body = "Your date of birth has been changed to " + data.DateOfBirth.Value.ToString("dd/MM/yyyy") }); } //Role if (string.IsNullOrWhiteSpace(data.Role) == false && original.Role.Title != data.Role) { Role r = dc.Roles.Where(x => x.Title == data.Role).FirstOrDefault(); if (r == null) { r = new Role { Title = data.Role }; dc.Roles.Add(r); n.Add(new Notification { User = original, Title = "Profile Update", Body = "Your role has been changed to " + data.Role.ToString() }); } original.Role = r; } //Address if (string.IsNullOrWhiteSpace(data.Address) == false && original.Address != data.Address) { original.Address = data.Address; n.Add(new Notification { User = original, Title = "Profile Update", Body = "Your address has been changed to " + data.Address }); } //Email if (string.IsNullOrWhiteSpace(data.Email) == false && original.Email != data.Email) { original.Email = data.Email; n.Add(new Notification { User = original, Title = "Profile Update", Body = "Your email has been changed to " + data.Email }); } //NoK1 if (string.IsNullOrWhiteSpace(data.NextOfKin1) == false && original.NextOfKin1 != data.NextOfKin1) { n.Add(new Notification { User = original, Title = "Profile Update", Body = "Your next of kin has been changed to " + data.NextOfKin1 }); original.NextOfKin1 = data.NextOfKin1; } //NoK2 if (string.IsNullOrWhiteSpace(data.NextOfKin2) == false && original.NextOfKin2 != data.NextOfKin2) { n.Add(new Notification { User = original, Title = "Profile Update", Body = "Your next of kin has been changed to " + data.NextOfKin2 }); original.NextOfKin2 = data.NextOfKin2; } //Marital Status if (string.IsNullOrWhiteSpace(data.MaritalStatus) == false) { MaritalStatus m = dc.MaritalStatuses.Where(x => x.MaritalStatusName == data.MaritalStatus).FirstOrDefault(); if (m == null) { dc.MaritalStatuses.Add(new MaritalStatus { MaritalStatusName = data.MaritalStatus }); } n.Add(new Notification { User = original, Title = "Profile Update", Body = "Your marital status has been changed to " + data.MaritalStatus }); original.MaritalStatus = m; } //Nationality if (string.IsNullOrWhiteSpace(data.Nationality) == false && original.Nationality != data.Nationality) { n.Add(new Notification { User = original, Title = "Profile Update", Body = "Your nationality has been changed to " + data.Nationality }); original.Nationality = data.Nationality; } //Visa Status if (string.IsNullOrWhiteSpace(data.VisaStatus) == false && original.VisaStatus != data.VisaStatus) { n.Add(new Notification { User = original, Title = "Profile Update", Body = "Your visa status has been changed to " + data.VisaStatus }); original.VisaStatus = data.VisaStatus; } //Gender if (string.IsNullOrWhiteSpace(data.Gender) == false && original.Gender != data.Gender) { n.Add(new Notification { User = original, Title = "Profile Update", Body = "Your gender has been changed to " + data.Gender }); original.Gender = data.Gender; } //Medical Status if (string.IsNullOrWhiteSpace(data.MedicalStatus) == false && original.MedicalStatus != data.MedicalStatus) { n.Add(new Notification { User = original, Title = "Profile Update", Body = "Your medical status has been changed to " + data.MedicalStatus }); original.MedicalStatus = data.MedicalStatus; } //UserLanguages if (data.Languages != null) { dc.UserLanguages.Include(x => x.Language).Where(x => x.User.UserID == data.UserID).ToList().ForEach(x => { if (!data.Languages.Contains(x.Language.LanguageName)) { dc.UserLanguages.Remove(x); } }); foreach (var language in data.Languages) { Language l = dc.Languages.Where(x => x.LanguageName == language).FirstOrDefault(); if (l == null) { l = new Language { LanguageName = language }; dc.Languages.Add(l); } UserLanguage ul = dc.UserLanguages.Include(x => x.Language).Include(x => x.User).Where(x => x.Language == l && x.User.UserID == data.UserID).FirstOrDefault(); if (ul == null) { n.Add(new Notification { User = original, Title = "Profile Update", Body = "A new language has been added " + data.Languages.ToString() }); dc.UserLanguages.Add(new UserLanguage { Language = l, User = dc.User.Where(x => x.UserID == data.UserID).FirstOrDefault() }); } } } //User Skills if (data.Skills != null) { dc.UserSkills.Include(x => x.Skill).Where(x => x.User.UserID == data.UserID).ToList().ForEach(x => { if (!data.Skills.Contains(x.Skill.SkillName)) { dc.UserSkills.Remove(x); } }); foreach (var skill in data.Skills) { Skill s = dc.Skills.Where(x => x.SkillName == skill).FirstOrDefault(); if (s == null) { s = new Skill { SkillName = skill }; dc.Skills.Add(s); } UserSkill us = dc.UserSkills.Include(x => x.Skill).Include(x => x.User).Where(x => x.Skill == s && x.User.UserID == data.UserID).FirstOrDefault(); if (us == null) { n.Add(new Notification { User = original, Title = "Profile Update", Body = "A new skill has been added " + data.Skills.ToString() }); dc.UserSkills.Add(new UserSkill { Skill = s, User = dc.User.Where(x => x.UserID == data.UserID).FirstOrDefault() }); } } } original.DateTimeUpdated = DateTime.Now; if (n.Count == 1) { NotificationHandler.SendNotification(n.First(), dc); } else if (n.Count > 1) { NotificationHandler.SendNotification(new Notification { Title = "Profile Update", Body = "Your profile has been udpated", User = original }, dc); } //Saves the changes. dc.SaveChanges(); } return(new JsonResult(data)); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null || requester.UserType.UserTypeName != "Admin") { return(new StatusCodeResult(403)); } List <string> errors = new List <string>(); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); CreateTeamModel data; try { data = JsonConvert.DeserializeObject <CreateTeamModel>(requestBody); } catch { return(new BadRequestResult()); } if (string.IsNullOrWhiteSpace(requestBody) || data == null) { return(new BadRequestResult()); } using (DataContext dc = new DataContext()) { if (string.IsNullOrWhiteSpace(data.TeamName)) { errors.Add("Team name cannot be empty"); } else { Team t = dc.Team.Where(x => x.TeamName == data.TeamName).FirstOrDefault(); if (t != null) { errors.Add("Team with that name already exists"); } } User u = null; if (!string.IsNullOrEmpty(data.LeaderName)) { u = dc.User.Where(x => x.Name.ToLower() == data.LeaderName.ToLower()).FirstOrDefault(); if (u == null) { errors.Add("The specified leader does not exist"); } } if (errors.Count > 0) { return(new BadRequestObjectResult(errors)); } User user = dc.User.Include(x => x.Team).Where(x => x.Name.ToLower() == data.LeaderName.ToLower()).FirstOrDefault(); if (user != null && user.Team != null) { Team removeManager = dc.Team.Where(x => x.TeamID == user.Team.TeamID).FirstOrDefault(); if (removeManager != null) { removeManager.LeaderID = null; } } Team team = new Team { TeamName = data.TeamName, ProjectName = data.ProjectName }; if (u != null) { team.LeaderID = u.UserID; u.Team = team; } dc.Team.Add(team); dc.SaveChanges(); return(new OkResult()); } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null) { return(new StatusCodeResult(403)); } List <string> errors = new List <string>(); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); UserIDModel data; CreateUserModel user; try { data = JsonConvert.DeserializeObject <UserIDModel>(requestBody); } catch { return(new BadRequestResult()); } if (string.IsNullOrWhiteSpace(requestBody) || data == null) { return(new BadRequestResult()); } using (DataContext dc = new DataContext()) { User u = dc.User.Include(x => x.UserLanguages).ThenInclude(x => x.Language).Include(x => x.UserSkills).ThenInclude(x => x.Skill).Include(x => x.Documents).Include(x => x.Team).Include(x => x.MaritalStatus).Include(x => x.Role).Where(x => x.UserID == data.UserID).FirstOrDefault(); if (u == null) { errors.Add("User does not exist"); } if (errors.Count > 0) { return(new BadRequestObjectResult(errors)); } String MaritalStatus = null; if (u.MaritalStatus != null) { MaritalStatus = u.MaritalStatus.MaritalStatusName; } user = new CreateUserModel { Address = u.Address, DateOfBirth = u.DateOfBirth, Email = u.Email, Gender = u.Gender, Languages = u.UserLanguages.Select(x => x.Language.LanguageName).ToList(), Skills = u.UserSkills.Select(x => x.Skill.SkillName).ToList(), MaritalStatus = MaritalStatus, MedicalStatus = u.MedicalStatus, Mobile = u.Mobile, Name = u.Name, Nationality = u.Nationality, NextOfKin1 = u.NextOfKin1, NextOfKin2 = u.NextOfKin2, Role = u.Role.Title, VisaStatus = u.VisaStatus, UserID = u.UserID, TeamID = u.Team?.TeamID.ToString(), Documents = u.Documents.Where(x => !x.IsProfilePicture).Select(x => x.FileName.ToString()).ToList() }; Document d = u.Documents.Where(x => x.IsProfilePicture).SingleOrDefault(); if (d != null) { var webClient = new WebClient(); byte[] f = webClient.DownloadData(d.FileLocation); user.ProfilePicture = f; } } return(new JsonResult(user)); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { req.Headers.TryGetValue("sessionToken", out StringValues sessionToken); User requester = SessionValidator.ValidateSession(sessionToken.ToString()); if (requester == null || requester.UserType.UserTypeName != "Admin") { return(new StatusCodeResult(403)); } List <string> errors = new List <string>(); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); UserIDModel data; User user; try { data = JsonConvert.DeserializeObject <UserIDModel>(requestBody); } catch { return(new BadRequestResult()); } if (string.IsNullOrWhiteSpace(requestBody) || data == null) { return(new BadRequestResult()); } using (DataContext dc = new DataContext()) { User u = dc.User.Where(x => x.UserID == data.UserID).FirstOrDefault(); if (u == null) { errors.Add("User does not exist"); } if (errors.Count > 0) { return(new BadRequestObjectResult(errors)); } Team t = dc.Team.Where(x => x.LeaderID == u.UserID).FirstOrDefault(); if (t != null) { t.LeaderID = null; } IEnumerable <UserSkill> uss = dc.UserSkills.Where(X => X.User == u); foreach (UserSkill us in uss) { dc.UserSkills.Remove(us); } IEnumerable <UserLanguage> uls = dc.UserLanguages.Where(X => X.User == u); foreach (UserLanguage ul in uls) { dc.UserLanguages.Remove(ul); } IEnumerable <Document> docs = dc.Documents.Where(X => X.User == u); foreach (Document doc in docs) { dc.Documents.Remove(doc); } //login credentials removed is user is deleted Authentication login = dc.Authentication.Where(x => x.User.Email == u.Email).FirstOrDefault(); if (login != null) { dc.Authentication.Remove(login); } IEnumerable <Notification> notifications = dc.Notification.Where(x => x.User == u); foreach (Notification n in notifications) { dc.Notification.Remove(n); } user = u; dc.Remove(user); dc.SaveChanges(); } return(new OkResult()); }