Пример #1
0
        public HttpResponseMessage AllTasksForProject(UserInProject user, [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           usersProjects = mongoDb.GetCollection <UsersProjects>(MongoCollections.UsersInProjects);
            UsersProjects userInProject = usersProjects.FindOneAs <UsersProjects>(Query.And(
                                                                                      Query.EQ("ProjectName", user.ProjectName),
                                                                                      Query.EQ("Username", sqlUser.Username),
                                                                                      Query.EQ("OrganizationName", user.OrganizationName)));

            if (userInProject == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No such organization, project, or user does not participate in them.");
                return(responseMessage);
            }

            var tasksCollection = mongoDb.GetCollection <ProjectTask>(MongoCollections.Tasks);

            var openTasks      = tasksCollection.AsQueryable <ProjectTask>().Where(x => x.ProjectName == user.ProjectName && x.Completed == false);
            var completedTasks = tasksCollection.AsQueryable <ProjectTask>().Where(x => x.ProjectName == user.ProjectName && x.Completed == true);

            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK, new { Open = openTasks, Completed = completedTasks }));
        }
        public HttpResponseMessage PostIssue(OpenIssue postedIssue, [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string authKey)
        {
            HttpResponseMessage responseMessage;
            User sqlUser;

            if (!ValidateCredentials.AuthKeyIsValid(db, authKey, out sqlUser) || postedIssue.Title.Length > 20 || postedIssue.Text.Length > 300)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid information.");
                return(responseMessage);
            }

            MongoCollection <UsersProjects> usersInProjects = mongoDb.GetCollection <UsersProjects>(MongoCollections.UsersInProjects);

            // todo projects need to be recognized by id
            // the relation table has to save the id and use it in further queries

            UsersProjects postingUser = usersInProjects.AsQueryable <UsersProjects>()
                                        .FirstOrDefault(x => x.Username == sqlUser.Username &&
                                                        x.ProjectName == postedIssue.ProjectName);

            if (postingUser == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "User does not participate in project.");
                return(responseMessage);
            }

            MongoCollection <OpenIssue> issuesCollection = mongoDb.GetCollection <OpenIssue>(MongoCollections.Issues);

            postedIssue.ByUser  = sqlUser.Username;
            postedIssue.Answers = new List <AnswerIssue>();
            issuesCollection.Save(postedIssue);

            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK, new { Posted = "Success" }));
        }
        public HttpResponseMessage SeeInvitations([ValueProvider(typeof(HeaderValueProviderFactory <string>))] string authKey)
        {
            HttpResponseMessage responseMessage;

            if (!ValidateCredentials.AuthKeyIsValid(db, authKey))
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid information.");
                return(responseMessage);
            }

            var user = db.Users.All().Single(x => x.AuthKey == authKey);

            var invitations = mongoDb.GetCollection(MongoCollections.Invitations);

            var foundInvitations = invitations.AsQueryable <Invitation>()
                                   .Where(x => x.InvitedUserId == new ObjectId(user.MongoId))
                                   .Select(x => new InvitationViewModel()
            {
                Id = x.Id.ToString(),
                OrganizationName = x.OrganizationName
            });

            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK,
                                                                 new { Invitations = foundInvitations }));
        }
        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 HttpResponseMessage UserProfile(UserProfile 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);
            }
            //todo check if user is in organization
            var queriedOrganization = GenericQueries.CheckOrganizationName(postData.OrganizationName, mongoDb);

            if (queriedOrganization == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid organization name.");
                return(responseMessage);
            }

            MongoCollection <UsersProjects> usersProjects = mongoDb.GetCollection <UsersProjects>(MongoCollections.UsersInProjects);

            var projectsInvolved = (from up in usersProjects.AsQueryable <UsersProjects>()
                                    where up.Username == sqlUser.Username && up.OrganizationName == postData.OrganizationName
                                    select up.ProjectName
                                    );

            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK, new { Projects = projectsInvolved }));
        }
        public HttpResponseMessage AcceptInvitation(InvitationViewModel acceptedInvitaion, [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string authKey)
        {
            HttpResponseMessage responseMessage;

            if (!ValidateCredentials.AuthKeyIsValid(db, authKey))
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid information.");
                return(responseMessage);
            }

            var user = db.Users.All().Single(x => x.AuthKey == authKey);

            var invitations = mongoDb.GetCollection(MongoCollections.Invitations);

            var foundInvitation = FindInvitation(acceptedInvitaion, user, invitations);

            if (foundInvitation == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid invitation.");
                return(responseMessage);
            }

            var organizations = mongoDb.GetCollection(MongoCollections.Organizations);

            var organization = organizations.FindOneAs <Organization>(Query.EQ("Name", foundInvitation.OrganizationName));

            CreateUserOrganizationRelation(organization, user, UserRoles.JuniorEmployee, mongoDb);

            invitations.Remove(Query.EQ("_id", foundInvitation.Id));

            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK,
                                                                 new { Accepted = "Success" }));
        }
        public HttpResponseMessage PostNote(Note postedNote, [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);
            }

            MongoCollection <UsersProjects> usersInProjects = mongoDb.GetCollection <UsersProjects>(MongoCollections.UsersInProjects);

            // todo projects need to be recognized by id
            // the relation table has to save the id and use it in further queries

            UsersProjects postingUser = usersInProjects.AsQueryable <UsersProjects>()
                                        .FirstOrDefault(x => x.Username == sqlUser.Username &&
                                                        x.ProjectName == postedNote.ProjectName && x.Role >= UserRoles.ProjectManager);

            if (postingUser == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "User does not participate in project or role is insufficient.");
                return(responseMessage);
            }

            MongoCollection <Note> notesCollection = mongoDb.GetCollection <Note>(MongoCollections.Notes);

            notesCollection.Save(postedNote);

            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK, new { Posted = "Success" }));
        }
Пример #8
0
        public HttpResponseMessage RemoveFromTask(ProjectTask task, [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 tasksCollection = mongoDb.GetCollection <ProjectTask>(MongoCollections.Tasks);
            var foundTask       = tasksCollection.FindOneAs <ProjectTask>(Query.EQ("_id", new ObjectId(task.TaskStringId)));

            if (foundTask == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Task does not exist.");
                return(responseMessage);
            }

            foundTask.UsersParticipating.Remove(sqlUser.Username);
            tasksCollection.Save(foundTask);

            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK, new { Edited = "Success" }));
        }
        public HttpResponseMessage PostAnswerToIssue(AnswerIssue answer, [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string authKey)
        {
            HttpResponseMessage responseMessage;
            User sqlUser;

            if (!ValidateCredentials.AuthKeyIsValid(db, authKey, out sqlUser) || answer.Text.Length > 300)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid information.");
                return(responseMessage);
            }

            MongoCollection <UsersProjects> usersInProjects = mongoDb.GetCollection <UsersProjects>(MongoCollections.UsersInProjects);

            // todo projects need to be recognized by id
            // the relation table has to save the id and use it in further queries



            MongoCollection <OpenIssue> issuesCollection = mongoDb.GetCollection <OpenIssue>(MongoCollections.Issues);
            var issue = issuesCollection.AsQueryable <OpenIssue>().FirstOrDefault(x => x.Id == new ObjectId(answer.IssueId));

            if (issue == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No such issue.");
                return(responseMessage);
            }

            issue.Answers.Add(answer);
            issuesCollection.Save(issue);

            List <AnswerIssue> entries = GetEntries(issue);

            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK, new { Entries = entries }));
        }
Пример #10
0
        public HttpResponseMessage RecentEvents(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 name.");
                return(responseMessage);
            }

            MongoCollection <UsersOrganizations> usersAndOrganizations = mongoDb.GetCollection <UsersOrganizations>(MongoCollections.UsersInOrganizations);
            UsersOrganizations foundUser;

            GenericQueries.CheckUser(authKey, queriedOrganization, usersAndOrganizations, out foundUser, UserRoles.OrganizationManager, db);

            if (foundUser == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Insufficient permissions.");
                return(responseMessage);
            }

            string recentEvents = HistoryController.GetEventsForOrganization(postData.OrganizationName);

            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK,
                                                                 new { Events = recentEvents.ToString() }));
        }
        public HttpResponseMessage GetNote(string noteId, [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);
            }

            MongoCollection <Note> notesCollection = mongoDb.GetCollection <Note>(MongoCollections.Notes);
            Note foundNote = notesCollection.FindOneAs <Note>(Query.EQ("_id", new ObjectId(noteId)));

            MongoCollection <UsersProjects> usersInProjects = mongoDb.GetCollection <UsersProjects>(MongoCollections.UsersInProjects);

            // todo projects need to be recognized by id
            UsersProjects postingUser = usersInProjects.AsQueryable <UsersProjects>()
                                        .FirstOrDefault(x => x.Username == sqlUser.Username &&
                                                        x.ProjectName == foundNote.ProjectName);

            if (postingUser == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "User does not participate in project.");
                return(responseMessage);
            }

            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK, new { Note = foundNote }));
        }
Пример #12
0
        public HttpResponseMessage CreateOrganization(Organization organization, [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string authKey)
        {
            HttpResponseMessage responseMessage;

            if (!ModelState.IsValid || !ValidateCredentials.AuthKeyIsValid(db, authKey))
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid information.");
                return(responseMessage);
            }

            var organizationsCollection = mongoDb.GetCollection(MongoCollections.Organizations);

            var alreadyExists = organizationsCollection.FindAs <Organization>(Query.EQ("Name", organization.Name)).Count();

            if (alreadyExists > 0)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Organization with same name already exists");
                return(responseMessage);
            }

            organization.DateCreated = DateTime.Now;
            organization.ProjectsIdsInOrganization = new BsonDocument();
            organization.UsersIdsInOrganization    = new BsonDocument();

            organizationsCollection.Save <Organization>(organization);

            organization.ProjectsIdsInOrganization = null;
            organization.UsersIdsInOrganization    = null;

            GenericQueries.CreateUserOrganizationRelation(organization, authKey, UserRoles.OrganizationOwner, db, mongoDb);

            responseMessage = this.Request.CreateResponse(HttpStatusCode.OK, organization);

            return(responseMessage);
        }
Пример #13
0
        public HttpResponseMessage GetInvolvedOrganizations([ValueProvider(typeof(HeaderValueProviderFactory <string>))] string authKey)
        {
            HttpResponseMessage responseMessage;

            if (!ValidateCredentials.AuthKeyIsValid(db, authKey))
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid information.");
                return(responseMessage);
            }

            var userMongoId = db.Users.All().Single(x => x.AuthKey == authKey).MongoId;


            var usersAndOrganizations = mongoDb.GetCollection(MongoCollections.UsersInOrganizations);

            List <OgranizationListEntry> found = (from o in usersAndOrganizations.AsQueryable <UsersOrganizations>()
                                                  where o.UserId == new ObjectId(userMongoId)
                                                  select new OgranizationListEntry()
            {
                Name = o.OrganizationName,
                OrganizationId = o.OrganizationId,
                Role = o.Role
            }).ToList();

            return(responseMessage = this.Request.CreateResponse(
                       HttpStatusCode.OK, new { Organizations = found }));
        }
        public HttpResponseMessage RejectInvitation(InvitationViewModel rejectedInvitaion, [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string authKey)
        {
            HttpResponseMessage responseMessage;

            if (!ValidateCredentials.AuthKeyIsValid(db, authKey))
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid information.");
                return(responseMessage);
            }

            var user = db.Users.All().Single(x => x.AuthKey == authKey);

            var invitations = mongoDb.GetCollection(MongoCollections.Invitations);

            var foundInvitation = FindInvitation(rejectedInvitaion, user, invitations);

            if (foundInvitation == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid invitation.");
                return(responseMessage);
            }

            invitations.Remove(Query.EQ("_id", foundInvitation.Id));

            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK,
                                                                 new { Removed = "Success" }));
        }
        public HttpResponseMessage AllUsersInOrganization(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 queryingUser;

            GenericQueries.CheckUser(authKey, queriedOrganization, usersAndOrganizations, out queryingUser, UserRoles.OrganizationManager, db);

            if (queryingUser == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Insufficient permissions.");
                return(responseMessage);
            }


            var usersInOrganization = (from us in usersAndOrganizations.AsQueryable <UsersOrganizations>()
                                       where us.OrganizationId == queriedOrganization.Id
                                       select new UsersInOrganizationVM()
            {
                Role = us.Role,
                Username = us.Username
            }).ToList();


            Dictionary <string, List <UsersInOrganizationVM> > grouped = new Dictionary <string, List <UsersInOrganizationVM> >();

            foreach (var item in usersInOrganization)
            {
                string stringRole = ConvertRoleToString(item.Role);
                if (grouped.ContainsKey(stringRole))
                {
                    grouped[stringRole].Add(item);
                }
                else
                {
                    grouped.Add(stringRole, new List <UsersInOrganizationVM>());
                    grouped[stringRole].Add(item);
                }
            }

            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK,
                                                                 new { Keys = grouped.Keys, Users = grouped }));
        }
        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" }));
        }
        public HttpResponseMessage InviteToOrganization(Invitation invitation, [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(invitation.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 invitator;

            GenericQueries.CheckUser(authKey, queriedOrganization, usersAndOrganizations, out invitator, UserRoles.OrganizationManager, db);

            if (invitator == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Insufficient permissions.");
                return(responseMessage);
            }

            MongoCollection users = mongoDb.GetCollection(MongoCollections.Users);
            User            invited;

            GenericQueries.FindPersonalProfile(out invited, users, invitation.InvitedUser.ToLower());
            if (invited == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "User does not exist");
                return(responseMessage);
            }

            var invitationsCollection = mongoDb.GetCollection(MongoCollections.Invitations);

            //todo check if invitation already exists
            invitation.InvitedUserId = invited._MongoId;
            invitationsCollection.Save(invitation);


            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK,
                                                                 new { Success = "Invitation sent." }));
        }
        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 }));
        }
        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));
        }
Пример #20
0
        public HttpResponseMessage FullInfo(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 name.");
                return(responseMessage);
            }

            MongoCollection <UsersOrganizations> usersAndOrganizations = mongoDb.GetCollection <UsersOrganizations>(MongoCollections.UsersInOrganizations);

            UsersOrganizations foundUser;

            GenericQueries.CheckUser(authKey, queriedOrganization, usersAndOrganizations, out foundUser, UserRoles.OrganizationManager, db);

            if (foundUser == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Insufficient permissions.");
                return(responseMessage);
            }

            var projectsCollection = mongoDb.GetCollection(MongoCollections.Projects);

            int projectsInOrganization = projectsCollection.AsQueryable <Project>()
                                         .Count(x => x.OrganizationName == queriedOrganization.Name);
            int employeesCount = usersAndOrganizations.AsQueryable <UsersOrganizations>()
                                 .Count(x => x.OrganizationId == queriedOrganization.Id);

            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK,
                                                                 new
            {
                Employees = employeesCount.ToString(),
                Projects = projectsInOrganization.ToString()
            }));
        }
        public HttpResponseMessage GetIssue(ProjectPostData postData, [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string authKey)
        {
            // todo get issue needs only issue id
            HttpResponseMessage responseMessage;
            User sqlUser;

            if (!ValidateCredentials.AuthKeyIsValid(db, authKey, out sqlUser))
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid information.");
                return(responseMessage);
            }

            MongoCollection <UsersProjects> usersInProjects = mongoDb.GetCollection <UsersProjects>(MongoCollections.UsersInProjects);

            // todo projects need to be recognized by id
            // the relation table has to save the id and use it in further queries

            UsersProjects postingUser = usersInProjects.AsQueryable <UsersProjects>()
                                        .FirstOrDefault(x => x.Username == sqlUser.Username &&
                                                        x.ProjectName == postData.ProjectName);

            if (postingUser == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "User does not participate in project.");
                return(responseMessage);
            }

            MongoCollection <OpenIssue> issuesCollection = mongoDb.GetCollection <OpenIssue>(MongoCollections.Issues);
            var issue = issuesCollection.AsQueryable <OpenIssue>().FirstOrDefault(x => x.Id == new ObjectId(postData.IssueId));

            if (issue == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No such issue.");
                return(responseMessage);
            }

            List <AnswerIssue> entries = GetEntries(issue);

            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK, new { Entries = entries, Title = issue.Title }));
        }
        public HttpResponseMessage CreateProject(Project project, [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(project.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 userCreating;

            GenericQueries.CheckUser(authKey, queriedOrganization, usersAndOrganizations, out userCreating, UserRoles.OrganizationManager, db);

            if (userCreating == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Insufficient permissions.");
                return(responseMessage);
            }
            //todo check if project name is unique
            MongoCollection <BsonDocument> projects = mongoDb.GetCollection(MongoCollections.Projects);

            projects.Save(project);

            CreateUserProjectRelation(userCreating, project);
            HistoryController.RecordHistoryEntry(queriedOrganization.Name, userCreating.Username, string.Format("created project - {0}", project.Name));

            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK,
                                                                 new { Created = "Success" }));
        }
Пример #23
0
        public HttpResponseMessage CreateTask(ProjectTask task, [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string authKey)
        {
            HttpResponseMessage responseMessage;
            User sqlUser;

            if (!ValidateCredentials.AuthKeyIsValid(db, authKey, out sqlUser) ||
                task.TaskName.Length < 1 || task.TaskName.Length > 20 || task.TaskDescription.Length > 100)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid information.");
                return(responseMessage);
            }

            var           usersProjects = mongoDb.GetCollection <UsersProjects>(MongoCollections.UsersInProjects);
            UsersProjects userInProject = usersProjects.FindOneAs <UsersProjects>(Query.And(
                                                                                      Query.EQ("ProjectName", task.ProjectName),
                                                                                      Query.EQ("Username", sqlUser.Username),
                                                                                      Query.EQ("OrganizationName", task.OrganizationName)));

            if (userInProject == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No such organization, project, or user does not participate in them.");
                return(responseMessage);
            }
            if (userInProject.Role < UserRoles.ProjectManager)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Insufficient permissions.");
                return(responseMessage);
            }

            var tasksCollection = mongoDb.GetCollection <ProjectTask>(MongoCollections.Tasks);

            task.UsersParticipating = new List <string>();
            tasksCollection.Save(task);

            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK, new { Posted = "Success" }));
        }
        public HttpResponseMessage GetProjectInformation(ProjectPostData project, [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);
            }

            MongoCollection <UsersProjects> usersInProjects = mongoDb.GetCollection <UsersProjects>(MongoCollections.UsersInProjects);

            // todo projects need to be recognized by id
            // the relation table has to save the id and use it in further queries

            UsersProjects postingUser = usersInProjects.AsQueryable <UsersProjects>()
                                        .FirstOrDefault(x => x.Username == sqlUser.Username &&
                                                        x.ProjectName == project.ProjectName);

            if (postingUser == null)
            {
                responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "User does not participate in project.");
                return(responseMessage);
            }

            MongoCollection <OpenIssue>   issuesCollection = mongoDb.GetCollection <OpenIssue>(MongoCollections.Issues);
            MongoCollection <Note>        notesCollection  = mongoDb.GetCollection <Note>(MongoCollections.Notes);
            MongoCollection <ProjectTask> tasksCollection  = mongoDb.GetCollection <ProjectTask>(MongoCollections.Tasks);

            var issues = (from i in issuesCollection.AsQueryable <OpenIssue>()
                          where i.ProjectName == project.ProjectName
                          select new TableCell()
            {
                Id = i.Id.ToString(),
                Title = i.Title
            }).ToList();

            issues.Reverse();

            var notes = (from n in notesCollection.AsQueryable <Note>()
                         where n.ProjectName == project.ProjectName
                         select new TableCell()
            {
                Id = n.Id.ToString(),
                Title = n.Title
            }).ToList();

            var allTasksCount = tasksCollection.AsQueryable <ProjectTask>()
                                .Where(x => x.ProjectName == project.ProjectName).Count();
            var allOpenTasks = tasksCollection.AsQueryable <ProjectTask>()
                               .Where(x => x.Completed == false && x.ProjectName == project.ProjectName).Count();
            TableCell task = new TableCell()
            {
                Title = string.Format("All: {0}, Open: {1}", allTasksCount, allOpenTasks)
            };


            return(responseMessage = this.Request.CreateResponse(HttpStatusCode.OK,
                                                                 new { Issues = issues, Notes = notes, UserRoleInProject = postingUser.Role,
                                                                       Tasks = task }));
        }