Beispiel #1
0
        public static void AddAdmin(string misparIshi,
                                    bool isSuperAdmin,
                                    // Whether we should set isSuperAdmin if this admin already exists or
                                    // just leave the current value.
                                    bool forceNewAdminLevel = true)
        {
            if (!CurrentMisparIshi.IsSuperAdmin())
            {
                return;
            }
            var dataContext   = new AdminDataContext();
            var existingAdmin = dataContext.Admins
                                .Where(admin => admin.MisparIshi == misparIshi)
                                .FirstOrDefault();

            if (existingAdmin != null)
            {
                if (forceNewAdminLevel)
                {
                    existingAdmin.IsSuperAdmin = isSuperAdmin;
                }
            }
            else
            {
                var newAdmin = new Admin();
                newAdmin.MisparIshi   = misparIshi;
                newAdmin.IsSuperAdmin = isSuperAdmin;
                dataContext.Admins.InsertOnSubmit(newAdmin);
            }
            dataContext.SubmitChanges();
            AdminChangeWriter.WriteAdminChange(
                String.Format("Added admin {0}", misparIshi));
            CurrentMisparIshi.SetAdmins();
        }
Beispiel #2
0
        // Returns the queryable object to the DB.
        private static IQueryable <Person> getQueryable(
            DbRequest dbRequest,
            PersonDataContext dataContext)
        {
            var query = dataContext.Persons.AsQueryable();

            if (dbRequest.ForceMe &&
                CurrentMisparIshi.GetCurrentMisparIshi().Length > 0)
            {
                query = query.Where(person =>
                                    person.MisparIshi == CurrentMisparIshi.GetCurrentMisparIshi());
                return(query);
            }

            if (dbRequest.Tags > 1)
            {
                query = query.Where(person =>
                                    person.Tags % dbRequest.Tags == 0);
            }

            if (dbRequest.StandardInputTextValues.Count != 0)
            {
                // Note that WhereMatches is defined in WhereMatchesQuery.cs.
                query = query.WhereMatches(
                    dbRequest.StandardInputTextValues, dbRequest.IsOnlyNumbers);
            }
            return(query);
        }
Beispiel #3
0
        public static void DeleteTag(string tagName)
        {
            lock (AdminTagAdder.LOCK)
            {
                if (!CurrentMisparIshi.IsAdmin())
                {
                    return;
                }
                if (!TagToPrimeDictionary.TAG_NAME_TO_TAG.ContainsKey(tagName))
                {
                    return;
                }
                var tagToRemoveId = TagToPrimeDictionary.TAG_NAME_TO_TAG[tagName].PrimeId;

                var dataContext = new PersonDataContext();
                var biggestTag  = dataContext.TagPrimes
                                  .OrderByDescending(tag => tag.PrimeId)
                                  .FirstOrDefault();
                var biggestTagId = biggestTag.PrimeId;
                if (biggestTagId < 2)
                {
                    return;
                }

                // Ok so we've isolated the ID of the tag we want to remove R
                // and the ID biggest tag B. We now do the following:
                // (1) Change R's text to  " ". This is a bit of a hack but elsewhere
                // the code will ensure that " " is not searchable or rendered. Note
                // that we do this by deleting it and creating a new one.
                // (2) For every person with tag R, divide their tag value by R.ID.
                //     For every person with tag B, multiple their tag value by R.ID.
                // (3) Change R's text to B's text. Delete B.
                // (4) For every person with tag B, divide their tag value by B.ID.
                // (5) Reset the dictionaries.

                // Step (1).
                dataContext.makeTagToRemoveUnsearchable(tagToRemoveId);

                // Step (2)
                dataContext.RemoveTagIdFromAllPeople(tagToRemoveId);
                dataContext.Persons
                .Where(person => person.Tags % biggestTagId == 0)
                .ToList()
                .ForEach(person => { person.Tags *= tagToRemoveId; });
                dataContext.SubmitChanges();

                // Step (3)
                dataContext.SwitchTagId(biggestTagId, tagToRemoveId);

                // Step (4)
                dataContext.RemoveTagIdFromAllPeople(tagToRemoveId);

                // Step (5)
                TagToPrimeDictionary.ResetTagToPrimeDictionaries();

                AdminChangeWriter.WriteAdminChange(
                    String.Format("Deleted tag {0}", tagName));
            }
        }
 public static IEnumerable <object> GetLogDataForGal(string today)
 {
     if (!CurrentMisparIshi.IsAdmin())
     {
         return(new object[] { new { message = "Only for admins" } });
     }
     return(GetLogObjects(today == "today"));
 }
Beispiel #5
0
 public static IEnumerable <object> GetInitialMetadata()
 {
     return(new object[] {
         new {
             is_admin = CurrentMisparIshi.IsAdmin(),
             tags_to_add_grouped = TagsToAddGetter.GetTags(),
             me_as_person = MeGetter.GetMe(),
             non_admins_can_add_tags = true
         }
     });
 }
Beispiel #6
0
        public static IEnumerable <object> ProcessDashboardRequest(
            string dashboardRequestType,
            int numDays)
        {
            if (dashboardRequestType == null)
            {
                return(createEmptyResponse("Why are you sending me a null request??"));
            }
            if (!CurrentMisparIshi.IsSuperAdmin())
            {
                return(createEmptyResponse("Go away you non-super-admin, you."));
            }
            switch (dashboardRequestType)
            {
            case "total_users":
                return(LogDataForGal.GetLogDataForGal("1"));

            case "today_users":
                return(LogDataForGal.GetLogDataForGal("today"));

            case "last_days_usages":
                return(LastDays.GetUsageForLastDays(
                           numDays, false /* showUsersInsteadOfUsages */));

            case "last_days_users":
                return(LastDays.GetUsageForLastDays(
                           numDays, true /* showUsersInsteadOfUsages */));

            case "tag_searches":
                return(QueriesGetter.GetTagQueries());

            case "common_searches":
                return(QueriesGetter.GetRepeatedQueries());

            case "bug_reports":
                return(BugReports.GetAllBugReports());

            case "shares":
                return(Shares.GetShares());

            case "admin_changes":
                return(AdminChanges.GetAdminChanges());

            case "all_tags":
                return(AllTags.GetTags());
            }
            return(createEmptyResponse(String.Format(
                                           "{0} is not supported.", dashboardRequestType)));
        }
Beispiel #7
0
 public static IEnumerable <object> GetMe()
 {
     return(new PersonDataContext().Persons
            .Where(person =>
                   person.MisparIshi == CurrentMisparIshi.GetCurrentMisparIshi())
            .Select(person => new {
         name = person.GivenName,
         // TODO(Josh): Extract this is as it is common functionality.
         full_name = String.Format("{0} {1}", person.GivenName, person.Surname),
         mispar_ishi = person.MisparIshi,
         picture = person.Picture,
         activity_level = person.GetUserTagLevel(),
         department = person.Department
     })
            .ToList());
 }
Beispiel #8
0
        public static void WriteAdminChange(string description)
        {
            if (!CurrentMisparIshi.IsAdmin())
            {
                return;
            }
            var message = String.Format(
                "The admin {0} made the following change: {1}.",
                CurrentMisparIshi.GetCurrentMisparIshi(),
                description);
            var newAdminChange = new AdminChange();

            newAdminChange.Description = message;
            newAdminChange.TimeAdded   = DateTime.Now;
            var dataContext = new LogDataContext();

            dataContext.AdminChanges.InsertOnSubmit(newAdminChange);
            dataContext.SubmitChanges();
        }
Beispiel #9
0
        public static void DeleteAdmin(string misparIshi)
        {
            if (!CurrentMisparIshi.IsSuperAdmin())
            {
                return;
            }
            var dataContext   = new AdminDataContext();
            var existingAdmin = dataContext.Admins
                                .Where(admin => admin.MisparIshi == misparIshi)
                                .FirstOrDefault();

            if (existingAdmin == null || existingAdmin.IsSuperAdmin)
            {
                return;
            }
            dataContext.Admins.DeleteOnSubmit(existingAdmin);
            dataContext.SubmitChanges();
            AdminChangeWriter.WriteAdminChange(
                String.Format("Deleted admin {0}", misparIshi));
            CurrentMisparIshi.SetAdmins();
        }
Beispiel #10
0
        public static IEnumerable <object> GetTags()
        {
            var dataContext = new PersonDataContext();
            var isAdmin     = CurrentMisparIshi.IsAdmin();
            var tagsList    = dataContext.TagPrimes
                              .Where(tag => tag.AllowNonAdminsToAdd || isAdmin)
                              .Select(tag => new {
                tag  = tag.Tag,
                type = tag.Type
            })
                              .ToList();

            var groupedTags = tagsList.GroupBy(tag => tag.type);

            return(groupedTags.Select(tagGroup => new
            {
                type = tagGroup.Key,
                tags = tagGroup.Select(singleTag => new {
                    tag = singleTag.tag
                }).OrderBy(tagWrapper => tagWrapper.tag)
            }));
        }
Beispiel #11
0
        public static void AddPersonalField(
            string misparIshi,
            string value,
            string inputType)
        {
            var dataContext = new PersonDataContext();

            var personsFromDb = dataContext.Persons
                                .Where(person => person.MisparIshi.Equals(misparIshi))
                                .ToList();

            if (personsFromDb.Count() != 1)
            {
                return;
            }

            var personFromDb = personsFromDb.First();

            if (!CurrentMisparIshi.IsCurrentUserOrAdmin(
                    personFromDb.MisparIshi))
            {
                return;
            }

            switch (inputType)
            {
            case PersonalFieldAdder.WHAT_I_DO:
                personFromDb.WhatIDo = value;
                break;

            case ADD_MOBILE:
                var normailzedMobileNumber = value.ToValidMobileNumber();
                if (normailzedMobileNumber == null)
                {
                    return;
                }
                personFromDb.Mobile = normailzedMobileNumber;
                personFromDb.MobilePhoneIsManuallyInput = true;
                break;

            case ADD_WORK_NUMBER:
                var normailzedWorkNumber = value.ToValidNonMobileNumber();
                if (normailzedWorkNumber == null)
                {
                    return;
                }
                personFromDb.WorkPhone = normailzedWorkNumber;
                personFromDb.WorkPhoneIsManuallyInput = true;
                break;

            case ADD_OTHER_NUMBER:
                var normailzedOtherNumber = value.ToValidNonMobileNumber();
                if (normailzedOtherNumber == null)
                {
                    return;
                }
                personFromDb.OtherTelephone            = normailzedOtherNumber;
                personFromDb.OtherPhoneIsManuallyInput = true;
                break;

            case SEX:
                personFromDb.Sex = value;
                personFromDb.SexIsManuallyInput = true;
                break;
            }

            dataContext.SubmitChanges();

            if (CurrentMisparIshi.IsAdminButNotCurrentUser(misparIshi))
            {
                AdminChangeWriter.WriteAdminChange(
                    String.Format(
                        "Set field of type {0} for {1} to {2}",
                        inputType,
                        personFromDb.MisparIshi,
                        value));
            }
        }
Beispiel #12
0
 private bool getIsMe()
 {
     return(CurrentMisparIshi.GetCurrentMisparIshi().Equals(person.MisparIshi));
 }
        public static IEnumerable <object> AddTag(
            string tagToAdd, bool isTagForAnyone, string type)
        {
            lock (LOCK)
            {
                if (!CurrentMisparIshi.IsAdmin())
                {
                    return(createResponseObject(
                               "You're not an admin, what are you doing here??"));
                }

                if (tagToAdd.Contains(" "))
                {
                    return(createResponseObject(
                               "אסור להוסיף תגים עם רווחים"));
                }

                if (tagToAdd.Length == 0)
                {
                    return(createResponseObject("נו באמת..."));
                }

                if (type.Length == 0)
                {
                    return(createResponseObject("נא להוסיף סוג"));
                }


                var nextPrime = TagToPrimeDictionary.GetNextPrime();
                if (nextPrime == -1)
                {
                    return(createResponseObject(
                               "יש 10,000 תגים ויותר מזה לא נתמך"));
                }


                var dataContext         = new PersonDataContext();
                var alreadyExistingTags =
                    dataContext.TagPrimes.Where(tag => tag.Tag.Equals(tagToAdd));
                if (alreadyExistingTags.Count() > 0)
                {
                    return(createResponseObject(
                               String.Format("התג {0} כבר קיים", tagToAdd)));
                }

                var newTagPrime = new TagPrime();
                newTagPrime.PrimeId             = nextPrime;
                newTagPrime.Tag                 = tagToAdd;
                newTagPrime.AllowNonAdminsToAdd = isTagForAnyone;
                newTagPrime.Type                = type;

                dataContext.TagPrimes.InsertOnSubmit(newTagPrime);
                dataContext.SubmitChanges();

                TagToPrimeDictionary.ResetTagToPrimeDictionaries();

                AdminChangeWriter.WriteAdminChange(
                    String.Format("Added tag {0} of type {1}", tagToAdd, type));

                return(createResponseObject(
                           String.Format("התג {0} מסוג {1} התווסף בהצלחה", tagToAdd, type)));
            }
        }