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 }));
        }
示例#2
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 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 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 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 }));
        }
示例#9
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 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" }));
        }