Beispiel #1
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() }));
        }
Beispiel #2
0
        public HttpResponseMessage CompleteTask(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.Completed = true;
            tasksCollection.Save(foundTask);
            HistoryController.RecordHistoryEntry(foundTask.OrganizationName, sqlUser.Username, "task completed", foundTask.ProjectName);

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