public HttpResponseMessage UserAdminProfile(UserInProject postData, [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string authKey) { HttpResponseMessage responseMessage; if (!ValidateCredentials.AuthKeyIsValid(db, authKey)) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid information."); return(responseMessage); } var queriedOrganization = GenericQueries.CheckOrganizationName(postData.OrganizationName, mongoDb); if (queriedOrganization == null) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid organization name."); return(responseMessage); } MongoCollection <UsersOrganizations> usersAndOrganizations = mongoDb.GetCollection <UsersOrganizations>(MongoCollections.UsersInOrganizations); UsersOrganizations userAssigning; GenericQueries.CheckUser(authKey, queriedOrganization, usersAndOrganizations, out userAssigning, UserRoles.OrganizationManager, db); if (userAssigning == null) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Insufficient permissions."); return(responseMessage); } MongoCollection <Project> projectsCollection = mongoDb.GetCollection <Project>(MongoCollections.Projects); var allProjects = (from pr in projectsCollection.AsQueryable <Project>() where pr.OrganizationName == postData.OrganizationName select new ProjectForUser() { Name = pr.Name, } ).ToDictionary <ProjectForUser, string>(x => x.Name); MongoCollection <UsersProjects> usersProjects = mongoDb.GetCollection <UsersProjects>(MongoCollections.UsersInProjects); var projectsInvolved = (from up in usersProjects.AsQueryable <UsersProjects>() where up.Username == postData.Username select up ); foreach (var proj in projectsInvolved) { if (allProjects.ContainsKey(proj.ProjectName)) { var participating = allProjects[proj.ProjectName]; participating.UserParticipatesIn = true; participating.Role = proj.Role; } } UsersOrganizations usersProfile = usersAndOrganizations.AsQueryable <UsersOrganizations>() .FirstOrDefault(x => x.Username == postData.Username && x.OrganizationName == postData.OrganizationName); return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK, new { Projects = allProjects.Values, Role = usersProfile.Role })); }
public void ToStringTest() { UsersOrganizations usersOrganizations = new UsersOrganizations(OrganizationTests.GetSimpleOrganization(), UserTests.GetSimpleUser(), new DateTime(2000, 10, 10)); Assert.AreEqual("My Organization: Ansættelsesdato 10-10-2000", usersOrganizations.ToString(), "ToString returned wrong string"); usersOrganizations.EndDate = new DateTime(2001, 10, 10); Assert.AreEqual("My Organization: 10-10-2000 - 10-10-2001", usersOrganizations.ToString(), "ToString with 2 datetimes returned wrong string"); }
public void UsersOrganizationsTest() { //Test hiring date UsersOrganizations usersOrganizations = new UsersOrganizations(OrganizationTests.GetSimpleOrganization(), UserTests.GetSimpleUser(), new DateTime(2000, 10, 10)); Assert.ThrowsException <ArgumentException>(() => { usersOrganizations.EndDate = new DateTime(1999, 10, 10); }, "EndDate cannot be less than StartDate"); usersOrganizations.EndDate = new DateTime(2001, 10, 10); Assert.ThrowsException <ArgumentException>(() => { usersOrganizations.StartDate = new DateTime(2002, 10, 10); }, "StartDate cannot be higher than EndDate"); }
public HttpResponseMessage RemoveFromProject(UserInProject postData, [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string authKey) { HttpResponseMessage responseMessage; User sqlUser; if (!ValidateCredentials.AuthKeyIsValid(db, authKey, out sqlUser)) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid information."); return(responseMessage); } var queriedOrganization = GenericQueries.CheckOrganizationName(postData.OrganizationName, mongoDb); if (queriedOrganization == null) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid organization name."); return(responseMessage); } MongoCollection <UsersOrganizations> usersAndOrganizations = mongoDb.GetCollection <UsersOrganizations>(MongoCollections.UsersInOrganizations); UsersOrganizations userAssigning; GenericQueries.CheckUser(authKey, queriedOrganization, usersAndOrganizations, out userAssigning, UserRoles.OrganizationManager, db); if (userAssigning == null) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Insufficient permissions."); return(responseMessage); } UsersOrganizations userRemoved = usersAndOrganizations.FindOneAs <UsersOrganizations>(Query.EQ("Username", postData.Username)); if (userRemoved == null) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No such user in organization."); return(responseMessage); } //todo check if assigned user isnt already in project MongoCollection <Project> projectsCollection = mongoDb.GetCollection <Project>(MongoCollections.Projects); Project project; project = projectsCollection.FindOneAs <Project>(Query.EQ("Name", postData.ProjectName)); if (project == null) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid project."); return(responseMessage); } MongoCollection <UsersProjects> usersProjects = mongoDb.GetCollection <UsersProjects>(MongoCollections.UsersInProjects); usersProjects.Remove(Query.EQ("Username", userRemoved.Username), RemoveFlags.Single); HistoryController.RecordHistoryEntry(userAssigning.OrganizationName, userAssigning.Username, string.Format("removed {0} from {1}", userRemoved.Username, project.Name)); return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK, new { Removed = "Success" })); }
private void CreateUserProjectRelation(UsersOrganizations userAssigned, Project project) { MongoCollection <UsersProjects> usersProjects = mongoDb.GetCollection <UsersProjects>(MongoCollections.UsersInProjects); UsersProjects usersProjectsRelation = new UsersProjects() { ProjectId = project.Id, ProjectName = project.Name, //UserId = userAssigned.Id, Username = userAssigned.Username, OrganizationName = project.OrganizationName, Role = userAssigned.Role }; usersProjects.Save(usersProjectsRelation); }
public HttpResponseMessage ChangeUserRole(UserProfile postData, [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string authKey) { HttpResponseMessage responseMessage; if (!ValidateCredentials.AuthKeyIsValid(db, authKey)) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid information."); return(responseMessage); } var queriedOrganization = GenericQueries.CheckOrganizationName(postData.OrganizationName, mongoDb); if (queriedOrganization == null) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid organization name."); return(responseMessage); } MongoCollection <UsersOrganizations> usersAndOrganizations = mongoDb.GetCollection <UsersOrganizations>(MongoCollections.UsersInOrganizations); UsersOrganizations userAssigning; //UsersOrganizations usersProfile = usersAndOrganizations.FindOneAs<UsersOrganizations>(Query.EQ("Username", postData.Username)); UsersOrganizations usersProfile = usersAndOrganizations.AsQueryable <UsersOrganizations>() .FirstOrDefault(x => x.Username == postData.Username && x.OrganizationName == postData.OrganizationName); GenericQueries.CheckUser(authKey, queriedOrganization, usersAndOrganizations, out userAssigning, UserRoles.OrganizationManager, db); if (usersProfile == null) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No such user in organization."); return(responseMessage); } if (userAssigning == null || userAssigning.Role < usersProfile.Role) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Insufficient permissions."); return(responseMessage); } HistoryController.RecordHistoryEntry(userAssigning.OrganizationName, userAssigning.Username, string.Format("{0} {1}", usersProfile.Role < postData.UserRole ? "promoted" : "demoted", usersProfile.Username)); usersProfile.Role = postData.UserRole; usersAndOrganizations.Save(usersProfile); ChangeUserRoleInProjects(usersProfile); return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK)); }
public HttpResponseMessage SetProjectManager(SetAsProjectManager postData, [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string authKey) { HttpResponseMessage responseMessage; if (!ValidateCredentials.AuthKeyIsValid(db, authKey)) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid information."); return(responseMessage); } var queriedOrganization = GenericQueries.CheckOrganizationName(postData.OrganizationName, mongoDb); if (queriedOrganization == null) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid organization name."); return(responseMessage); } MongoCollection <UsersOrganizations> usersAndOrganizations = mongoDb.GetCollection <UsersOrganizations>(MongoCollections.UsersInOrganizations); UsersOrganizations userAssigning; UsersOrganizations usersProfile = usersAndOrganizations.FindOneAs <UsersOrganizations>(Query.EQ("Username", postData.Username)); GenericQueries.CheckUser(authKey, queriedOrganization, usersAndOrganizations, out userAssigning, UserRoles.OrganizationManager, db); if (usersProfile == null) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No such user in organization."); return(responseMessage); } if (userAssigning == null || userAssigning.Role < usersProfile.Role) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Insufficient permissions."); return(responseMessage); } MongoCollection <UsersProjects> usersProjects = mongoDb.GetCollection <UsersProjects>(MongoCollections.UsersInProjects); UsersProjects userInProject = usersProjects.FindOneAs <UsersProjects>(Query.And( Query.EQ("ProjectName", postData.ProjectName), Query.EQ("Username", postData.Username), Query.EQ("OrganizationName", postData.OrganizationName))); userInProject.Role = postData.SetAsManager ? UserRoles.ProjectManager : usersProfile.Role; usersProjects.Save(userInProject); HistoryController.RecordHistoryEntry(userAssigning.OrganizationName, userAssigning.Username, string.Format("{0} {1} as Project Manager", postData.SetAsManager ? "assigned" : "dissociated", userInProject.Username), userInProject.ProjectName); return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK, new { User = userInProject })); }
internal static void CreateUserOrganizationRelation(Organization organization, User sqlUser, UserRoles role, MongoDatabase mongoDb) { var usersCollection = mongoDb.GetCollection(MongoCollections.Users); var mongoUser = usersCollection.FindOne(Query.EQ("_id", new ObjectId(sqlUser.MongoId))); var usersOrganizations = mongoDb.GetCollection(MongoCollections.UsersInOrganizations); UsersOrganizations newRelation = new UsersOrganizations() { UserId = mongoUser["_id"].AsObjectId, OrganizationId = organization.Id, OrganizationName = organization.Name, Role = role }; usersOrganizations.Save(newRelation); }
internal static void CreateUserOrganizationRelation(Organization organization, string authKey, UserRoles role, IUoWData db, MongoDatabase mongoDb) { var sqlUser = db.Users.All().Single(x => x.AuthKey == authKey); var usersCollection = mongoDb.GetCollection(MongoCollections.Users); var mongoUser = usersCollection.FindOneAs <User>(Query.EQ("_id", new ObjectId(sqlUser.MongoId))); var usersOrganizations = mongoDb.GetCollection(MongoCollections.UsersInOrganizations); UsersOrganizations newRelation = new UsersOrganizations() { UserId = mongoUser._MongoId, OrganizationId = organization.Id, OrganizationName = organization.Name, Username = mongoUser.Username, Role = role }; usersOrganizations.Save(newRelation); }
private void ChangeUserRoleInProjects(UsersOrganizations user) { MongoCollection <UsersProjects> usersProjects = mongoDb.GetCollection <UsersProjects>(MongoCollections.UsersInProjects); IMongoQuery query; if (user.Role < UserRoles.ProjectManager) { query = Query.And( Query.NE("Role", UserRoles.ProjectManager), Query.EQ("Username", user.Username) ); } else { query = Query.And( Query.EQ("Username", user.Username) ); } var update = Update.Set("Role", user.Role); usersProjects.Update(query, update, UpdateFlags.Multi); }
public ActionResult Edit(UserEditorModel viewModel) { //Get user MeetupModel model = new MeetupModel(); int? userId = this.UserId(); if (userId == null) { return(RedirectToAction("Index", "HomeController")); } User editUser = model.Users.SingleOrDefault(userInfo => userInfo.Id == userId); //Get businesses list if (string.IsNullOrEmpty(viewModel.ChosenBusinesses)) { viewModel.ChosenBusinesses = ""; } string[] selectedBusnisses = viewModel.ChosenBusinesses.Split(','); model.UsersBusinesses.RemoveRange(editUser.UsersBusinesses.Where(b => !selectedBusnisses.Contains(b.Business.Name))); List <Business> businesses = model.Businesses.Where(b => selectedBusnisses.Contains(b.Name)).ToList(); businesses = businesses.Where(b => !editUser.UsersBusinesses.Any(ub => ub.BusinessId == b.Id)).ToList(); foreach (Business business in businesses) { editUser.UsersBusinesses.Add(new UsersBusiness(business, editUser)); } //Get interests list if (string.IsNullOrEmpty(viewModel.ChosenInterests)) { viewModel.ChosenInterests = ""; } string[] selectedInterests = viewModel.ChosenInterests.Split(','); model.UsersInterests.RemoveRange(editUser.UsersInterests.Where(i => !selectedInterests.Contains(i.Interest.Name))); List <Interest> interests = model.Interests.Where(i => selectedInterests.Contains(i.Name)).ToList(); interests = interests.Where(i => !editUser.UsersInterests.Any(ui => ui.InterestId == i.Id)).ToList(); foreach (Interest interest in interests) { editUser.UsersInterests.Add(new UsersInterest(interest, editUser)); } //Make sure model is valid if (!ModelState.IsValid) { return(ReturnEdit(viewModel, editUser, model)); } else { if (!(viewModel.Organizations is null)) { //Create organizations list for (int i = 0; i < viewModel.Organizations.Count; i++) { OrganizationModel organization = viewModel.Organizations[i]; if (organization.State == "removed") { //Remove removed organizations UsersOrganizations removeOrganization = editUser.UsersOrganizations.SingleOrDefault(o => o.Id == organization.Id); if (!(removeOrganization is null)) { model.UsersOrganizations.Remove(removeOrganization); } } else if (organization.State == "old" || organization.State == "new") { //Make sure organization is in a valid state if (string.IsNullOrWhiteSpace(organization.Name)) { ModelState.AddModelError("Organizations[" + i + "].Name", "Feltet Organisation navn skal udfyldes."); return(ReturnEdit(viewModel, editUser, model)); } if (organization.StartDate is null) { ModelState.AddModelError("Organizations[" + i + "].StartDate", "Feltet ansættelsesdato skal udfyldes."); return(ReturnEdit(viewModel, editUser, model)); } if (organization.StartDate > DateTime.Now) { ModelState.AddModelError("Organizations[" + i + "].StartDate", "Ansættelsesdato må ikke være i fremtiden."); return(ReturnEdit(viewModel, editUser, model)); } else if (!(organization.EndDate is null)) { if (organization.StartDate.Value >= organization.EndDate) { ModelState.AddModelError("Organizations[" + i + "].StartDate", "Ansættelsesdato må ikke være efter s**t datoen."); return(ReturnEdit(viewModel, editUser, model)); } else if (organization.EndDate > DateTime.Now) { ModelState.AddModelError("Organizations[" + i + "].EndDate", "S**t datoen kan ikke være i fremtiden."); return(ReturnEdit(viewModel, editUser, model)); } } //Get the organization the user is editing or create a new one latter UsersOrganizations editOrganization = null; Organization newOrganization = null; Organization oldOrganization = null; if (organization.State == "old") { editOrganization = editUser.UsersOrganizations.SingleOrDefault(o => o.Id == organization.Id); if (editOrganization is null) { continue; } oldOrganization = editOrganization.Organization; } else if (organization.State != "new") { continue; } //Check new organization name if (organization.State == "new" || organization.Name != oldOrganization.Name) { newOrganization = model.Organizations.SingleOrDefault(o => o.Name == organization.Name); if (newOrganization is null) { //Call api to check organization name try { if (Organization.NameExists(organization.Name)) { newOrganization = new Organization(organization.Name); } else { ModelState.AddModelError("Organizations[" + i + "].Name", "Organisationen \"" + organization.Name + "\" eksisterer ikke."); return(ReturnEdit(viewModel, editUser, model)); } } catch (WebException) { ModelState.AddModelError("Organizations[" + i + "].Name", "Fejlede i at bedømme organisation navnet. Prøv igen senere"); return(ReturnEdit(viewModel, editUser, model)); } } } //Create new organization object or fill old object if (editOrganization is null) { editOrganization = new UsersOrganizations(newOrganization, editUser, organization.StartDate.Value); model.UsersOrganizations.Add(editOrganization); } else { if (!(newOrganization is null)) { editOrganization.Organization = newOrganization; } editOrganization.StartDate = organization.StartDate.Value; } editOrganization.EndDate = organization.EndDate; } } } if (!(viewModel.Picture is null)) { //Update profile picture if (!viewModel.Picture.ContentType.Contains("image")) { ModelState.AddModelError("Picture", "Feltet \"Profil Billed\" accepterer kun billeder."); return(ReturnEdit(viewModel, editUser, model)); } editUser.PictureUri = "data:image/png;base64," + viewModel.Picture.PictureFileToString(); } //Update everything else editUser.Description = viewModel.Description; editUser.Address.CityName = viewModel.Address.City; editUser.Address.Country = viewModel.Address.Country; editUser.Address.StreetName = viewModel.Address.StreetName; editUser.Address.StreetNumber = viewModel.Address.StreetNumber; editUser.Address.ZipCode = Convert.ToInt32(viewModel.Address.CityZipCode); editUser.FirstName = viewModel.FirstName; editUser.LastName = viewModel.LastName; model.SaveChanges(); return(RedirectToAction("Profile", "Users", new { editUser.Id })); } }
internal static void CheckUser(string authKey, Organization queriedOrganization, MongoCollection <UsersOrganizations> usersAndOrganizations, out UsersOrganizations foundUser, UserRoles role, IUoWData db) { var userMongoId = db.Users.All().Single(x => x.AuthKey == authKey).MongoId; foundUser = usersAndOrganizations.AsQueryable <UsersOrganizations>() .FirstOrDefault(x => x.Role >= role && x.UserId == new ObjectId(userMongoId) && x.OrganizationName == queriedOrganization.Name); }