Beispiel #1
0
 public static GpgPublicKeyInfo GetPublicKey(this ServiceAddress address)
 {
     return(address.Contact.Value.PublicKeys
            .Where(k => k.Type.Value == PublicKeyType.OpenPGP)
            .Select(k => CheckPublicKey(address, k))
            .FirstOrDefault(k => k != null));
 }
 public ServiceAddressEditViewModel(Translator translator, IDatabase db, ServiceAddress address)
     : this(translator, address.Service.Value)
 {
     Method   = "edit";
     Id       = address.Id.ToString();
     Address  = address.Address.Value.EscapeHtml();
     Service  = ((int)address.Service.Value).ToString();
     Category = ((int)address.Category.Value).ToString();
     Categories.Add(new NamedIntViewModel(translator, AddressCategory.Home, address.Category.Value == AddressCategory.Home));
     Categories.Add(new NamedIntViewModel(translator, AddressCategory.Mobile, address.Category.Value == AddressCategory.Mobile));
     Categories.Add(new NamedIntViewModel(translator, AddressCategory.Work, address.Category.Value == AddressCategory.Work));
 }
Beispiel #3
0
        private static GpgPublicKeyInfo CheckPublicKey(ServiceAddress address, PublicKey key)
        {
            var gpg     = new GpgWrapper(GpgWrapper.LinuxGpgBinaryPath, Global.Config.GpgHomedir);
            var keyInfo = gpg.ImportKeys(key.Data.Value).FirstOrDefault();

            if (keyInfo.Status != GpgKeyStatus.Active)
            {
                return(null);
            }

            if (!keyInfo.Uids.Any(u => u.Mail == address.Address.Value &&
                                  u.Trust <= GpgTrust.Marginal))
            {
                return(null);
            }

            return(new GpgPublicKeyInfo(keyInfo.Id));
        }
Beispiel #4
0
        public ServiceAddressViewModel(Translator translator, ServiceAddress address)
        {
            Id   = address.Id.ToString();
            Text = address.Address.Value.EscapeHtml();

            switch (address.Service.Value)
            {
            case ServiceType.EMail:
                PhraseDeleteConfirmationQuestion = translator.Get("Contact.Detail.Master.Mail.Delete.Confirm.Question", "Delete E-Mail address confirmation question", "Do you really wish to delete E-Mail address {0}?", address.GetText(translator)).EscapeHtml();
                break;

            case ServiceType.Phone:
                PhraseDeleteConfirmationQuestion = translator.Get("Contact.Detail.Master.Phone.Delete.Confirm.Question", "Delete phone number confirmation question", "Do you really wish to delete phone number {0}?", address.GetText(translator)).EscapeHtml();
                break;

            default:
                throw new NotSupportedException();
            }
        }
Beispiel #5
0
        private Contact CreateContact(int number)
        {
            var female          = _rnd.Next(2) == 1;
            int middleNameCount = _rnd.Next(5);

            if (middleNameCount > 2)
            {
                middleNameCount = 0;
            }

            var contact = new Contact(Guid.NewGuid());

            contact.Language.Value = SelectLanguage();
            contact.LastName.Value = LastNames.Skip(_rnd.Next(LastNames.Count())).First();

            if (female)
            {
                contact.FirstName.Value = FemaleFirstNames.Skip(_rnd.Next(FemaleFirstNames.Count())).First();
                var middleNames = new List <string>();
                for (int i = 0; i < middleNameCount; i++)
                {
                    middleNames.Add(FemaleFirstNames.Skip(_rnd.Next(FemaleFirstNames.Count())).First());
                }
                contact.MiddleNames.Value = string.Join(" ", middleNames);
            }
            else
            {
                contact.FirstName.Value = MaleFirstNames.Skip(_rnd.Next(MaleFirstNames.Count())).First();
                var middleNames = new List <string>();
                for (int i = 0; i < middleNameCount; i++)
                {
                    middleNames.Add(MaleFirstNames.Skip(_rnd.Next(MaleFirstNames.Count())).First());
                }
                contact.MiddleNames.Value = string.Join(" ", middleNames);
            }

            contact.BirthDate.Value    = new DateTime(1960, 1, 1).AddDays(_rnd.NextDouble() * 40d * 365d);
            contact.Organization.Value = "New Organization";

            var homeMail = new ServiceAddress(Guid.NewGuid());

            homeMail.Service.Value    = ServiceType.EMail;
            homeMail.Category.Value   = AddressCategory.Home;
            homeMail.Precedence.Value = contact.ServiceAddresses.MaxOrDefault(a => a.Precedence.Value, 0) + 1;
            homeMail.Address.Value    =
                "stefan+" +
                contact.FirstName.Value.ToLowerInvariant() + "." +
                contact.LastName.Value.ToLowerInvariant() + "@savvy.ch";
            homeMail.Contact.Value = contact;

            if (_rnd.Next(3) != 0)
            {
                var mobile = new ServiceAddress(Guid.NewGuid());
                mobile.Service.Value    = ServiceType.Phone;
                mobile.Category.Value   = AddressCategory.Mobile;
                mobile.Precedence.Value = contact.ServiceAddresses.MaxOrDefault(a => a.Precedence.Value, 0) + 1;
                mobile.Address.Value    =
                    MobilePrefix.Skip(_rnd.Next(MobilePrefix.Count())).First() +
                    ComposeNumber();
                mobile.Contact.Value = contact;
            }

            if (_rnd.Next(3) == 0)
            {
                var phone = new ServiceAddress(Guid.NewGuid());
                phone.Service.Value    = ServiceType.Phone;
                phone.Category.Value   = AddressCategory.Mobile;
                phone.Precedence.Value = contact.ServiceAddresses.MaxOrDefault(a => a.Precedence.Value, 0) + 1;
                phone.Address.Value    =
                    PhonePrefix.Skip(_rnd.Next(PhonePrefix.Count())).First() +
                    ComposeNumber();
                phone.Contact.Value = contact;
            }

            var place         = Places.Skip(_rnd.Next(Places.Count())).First();
            var postalAddress = new PostalAddress(Guid.NewGuid());

            postalAddress.Country.Value    = GetCountry("Schweiz");
            postalAddress.State.Value      = GetState(place.Item3);
            postalAddress.PostalCode.Value = place.Item1.ToString();
            postalAddress.Place.Value      = place.Item2;
            postalAddress.Street.Value     =
                Streets.Skip(_rnd.Next(Streets.Count())).First() +
                " " + (_rnd.Next(23) + 1).ToString();
            postalAddress.Precedence.Value = contact.PostalAddresses.MaxOrDefault(a => a.Precedence.Value, 0) + 1;
            postalAddress.Contact.Value    = contact;

            var subscription = new Subscription(Guid.NewGuid());

            subscription.Contact.Value   = contact;
            subscription.Feed.Value      = GetFeed("Piratenpartei Zentralschweiz", null);
            subscription.StartDate.Value = new DateTime(2018, 12, 18);

            switch (_rnd.Next(4))
            {
            case 0:
                var subscription2 = new Subscription(Guid.NewGuid());
                subscription2.Contact.Value   = contact;
                subscription2.Feed.Value      = GetFeed("Piratenpartei Zug", "Piratenpartei Zentralschweiz");
                subscription2.StartDate.Value = new DateTime(2018, 12, 18);
                break;

            case 1:
                var subscription3 = new Subscription(Guid.NewGuid());
                subscription3.Contact.Value   = contact;
                subscription3.Feed.Value      = GetFeed("Piratenpartei Luzern", "Piratenpartei Zentralschweiz");
                subscription3.StartDate.Value = new DateTime(2018, 12, 18);
                break;
            }

            var tagAssignment1 = new TagAssignment(Guid.NewGuid());

            tagAssignment1.Tag.Value     = GetTag("Partizipationsmails", TagUsage.Mailing, TagMode.Default | TagMode.Manual);
            tagAssignment1.Contact.Value = contact;

            if (_rnd.Next(5) < 4)
            {
                var tagAssignment2 = new TagAssignment(Guid.NewGuid());
                tagAssignment2.Tag.Value     = GetTag("Verantstaltungsmails", TagUsage.Mailing, TagMode.Default | TagMode.Manual | TagMode.Self);
                tagAssignment2.Contact.Value = contact;
            }

            if (_rnd.Next(5) < 4)
            {
                var tagAssignment3 = new TagAssignment(Guid.NewGuid());
                tagAssignment3.Tag.Value     = GetTag("Aktionsmails", TagUsage.Mailing, TagMode.Default | TagMode.Manual | TagMode.Self);
                tagAssignment3.Contact.Value = contact;
            }

            if (_rnd.Next(5) < 3)
            {
                var tagAssignment4 = new TagAssignment(Guid.NewGuid());
                tagAssignment4.Tag.Value     = GetTag("Aktivist", TagUsage.None, TagMode.Manual);
                tagAssignment4.Contact.Value = contact;
            }

            _db.Save(contact);

            return(contact);
        }
        public ServiceAddressEdit()
        {
            this.RequiresAuthentication();

            Get["/serviceaddress/edit/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    address  = Database.Query <ServiceAddress>(idString);

                if (address != null)
                {
                    if (HasAccess(address.Contact.Value, PartAccess.Contact, AccessRight.Write))
                    {
                        return(View["View/serviceaddressedit.sshtml",
                                    new ServiceAddressEditViewModel(Translator, Database, address)]);
                    }
                }

                return(null);
            };
            Post["/serviceaddress/edit/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    model    = JsonConvert.DeserializeObject <ServiceAddressEditViewModel>(ReadBody());
                var    address  = Database.Query <ServiceAddress>(idString);
                var    status   = CreateStatus();

                if (status.ObjectNotNull(address))
                {
                    if (status.HasAccess(address.Contact.Value, PartAccess.Contact, AccessRight.Write))
                    {
                        status.AssignStringRequired("Address", address.Address, model.Address);
                        status.AssignEnumIntString("Category", address.Category, model.Category);

                        if (status.IsSuccess)
                        {
                            Database.Save(address);
                            switch (address.Service.Value)
                            {
                            case ServiceType.EMail:
                                Journal(address.Contact,
                                        "ServiceAddress.EMail.Journal.Edit",
                                        "Journal entry changed mail address",
                                        "Changed mail address {0}",
                                        t => address.GetText(t));
                                break;

                            case ServiceType.Phone:
                                Journal(address.Contact,
                                        "ServiceAddress.Phone.Journal.Edit",
                                        "Journal entry changed phone number",
                                        "Changed phone number {0}",
                                        t => address.GetText(t));
                                break;

                            default:
                                throw new NotSupportedException();
                            }
                        }
                    }
                }

                return(status.CreateJsonData());
            };
            Get["/serviceaddress/add/phone/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    contact  = Database.Query <Contact>(idString);

                if (contact != null)
                {
                    if (HasAccess(contact, PartAccess.Contact, AccessRight.Write))
                    {
                        return(View["View/serviceaddressedit.sshtml",
                                    new ServiceAddressEditViewModel(Translator, Database, contact, ServiceType.Phone)]);
                    }
                }

                return(null);
            };
            Get["/serviceaddress/add/mail/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    contact  = Database.Query <Contact>(idString);

                if (contact != null)
                {
                    if (HasAccess(contact, PartAccess.Contact, AccessRight.Write))
                    {
                        return(View["View/serviceaddressedit.sshtml",
                                    new ServiceAddressEditViewModel(Translator, Database, contact, ServiceType.EMail)]);
                    }
                }

                return(null);
            };
            Post["/serviceaddress/add/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    model    = JsonConvert.DeserializeObject <ServiceAddressEditViewModel>(ReadBody());
                var    contact  = Database.Query <Contact>(idString);
                var    status   = CreateStatus();

                if (status.ObjectNotNull(contact))
                {
                    var address = new ServiceAddress(Guid.NewGuid());
                    status.AssignStringRequired("Address", address.Address, model.Address);
                    status.AssignEnumIntString("Category", address.Category, model.Category);
                    status.AssignEnumIntString("Service", address.Service, model.Service);
                    address.Precedence.Value = contact.PostalAddresses.MaxOrDefault(a => a.Precedence.Value, 0) + 1;
                    address.Contact.Value    = contact;

                    if (status.IsSuccess)
                    {
                        Database.Save(address);
                        switch (address.Service.Value)
                        {
                        case ServiceType.EMail:
                            Journal(address.Contact,
                                    "ServiceAddress.EMail.Journal.Add",
                                    "Journal entry added mail address",
                                    "Added mail address {0}",
                                    t => address.GetText(t));
                            break;

                        case ServiceType.Phone:
                            Journal(address.Contact,
                                    "ServiceAddress.Phone.Journal.Add",
                                    "Journal entry added phone number",
                                    "Added phone number {0}",
                                    t => address.GetText(Translator));
                            break;

                        default:
                            throw new NotSupportedException();
                        }
                    }
                }

                return(status.CreateJsonData());
            };
            Get["/serviceaddress/delete/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    address  = Database.Query <ServiceAddress>(idString);
                var    status   = CreateStatus();

                if (status.ObjectNotNull(address))
                {
                    if (status.HasAccess(address.Contact.Value, PartAccess.Contact, AccessRight.Write))
                    {
                        Database.Delete(address);
                        switch (address.Service.Value)
                        {
                        case ServiceType.EMail:
                            Journal(address.Contact,
                                    "ServiceAddress.EMail.Journal.Delete",
                                    "Journal entry deleted mail address",
                                    "Deleted mail address {0}",
                                    t => address.GetText(Translator));
                            break;

                        case ServiceType.Phone:
                            Journal(address.Contact,
                                    "ServiceAddress.Phone.Journal.Delete",
                                    "Journal entry deleted phone number",
                                    "Deleted phone number {0}",
                                    t => address.GetText(Translator));
                            break;

                        default:
                            throw new NotSupportedException();
                        }
                    }
                }

                return(status.CreateJsonData());
            };
            Post["/serviceaddress/switch"] = parameters =>
            {
                var model  = JsonConvert.DeserializeObject <SwitchViewModel>(ReadBody());
                var source = Database.Query <ServiceAddress>(model.SourceId);
                var status = CreateStatus();

                if (status.ObjectNotNull(source) &&
                    Guid.TryParse(model.TargetId, out Guid targetId))
                {
                    if (status.HasAccess(source.Contact.Value, PartAccess.Contact, AccessRight.Write))
                    {
                        var target = source.Contact.Value.ServiceAddresses
                                     .FirstOrDefault(a => a.Id.Equals(targetId));

                        if (status.ObjectNotNull(target))
                        {
                            var sourcePrecedence = source.Precedence.Value;
                            var targetPrecedence = target.Precedence.Value;
                            source.Precedence.Value = targetPrecedence;
                            target.Precedence.Value = sourcePrecedence;

                            Database.Save(source.Contact);
                            switch (source.Service.Value)
                            {
                            case ServiceType.EMail:
                                Journal(source.Contact,
                                        "ServiceAddress.EMail.Journal.Switch",
                                        "Journal entry deleted mail address",
                                        "Switched mail addresses {0} and {1}",
                                        t => source.GetText(t),
                                        t => target.GetText(t));
                                break;

                            case ServiceType.Phone:
                                Journal(source.Contact,
                                        "ServiceAddress.Phone.Journal.Delete",
                                        "Journal entry deleted phone Switch",
                                        "Switched phone numbers {0} and {1}",
                                        t => source.GetText(t),
                                        t => target.GetText(t));
                                break;

                            default:
                                throw new NotSupportedException();
                            }
                        }
                    }
                }
                else
                {
                    status.SetErrorNotFound();
                }

                return(status.CreateJsonData());
            };
        }