コード例 #1
0
 public ExportEditViewModel(Translator translator, IDatabase db, Session session, Export export)
     : this(translator)
 {
     Method         = "edit";
     Id             = export.Id.ToString();
     Name           = export.Name.Value.EscapeHtml();
     SelectFeed     = string.Empty;
     SelectTag      = string.Empty;
     SelectLanguage = string.Empty;
     Feeds          = new List <NamedIdViewModel>(db
                                                  .Query <Feed>()
                                                  .Where(o => session.HasAccess(o, PartAccess.Demography, AccessRight.Read))
                                                  .Select(o => new NamedIdViewModel(translator, o, o == export.SelectFeed.Value))
                                                  .OrderBy(o => o.Name));
     if (session.HasSystemWideAccess(PartAccess.Demography, AccessRight.Read))
     {
         Feeds.Add(new NamedIdViewModel(translator.Get("Export.Edit.Field.SelectFeed.None", "No selection in the select feed field of the edit export page", "None"), false, export.SelectFeed.Value == null));
     }
     Tags = new List <NamedIdViewModel>(db
                                        .Query <Tag>()
                                        .Select(t => new NamedIdViewModel(translator, t, t == export.SelectTag.Value))
                                        .OrderBy(t => t.Name));
     Tags.Add(new NamedIdViewModel(translator.Get("Export.Edit.Field.SelectTag.None", "No selection in the select tag field of the edit export page", "None"), false, export.SelectTag.Value == null));
     Languages = new List <NamedIntViewModel>();
     Languages.Add(new NamedIntViewModel(translator, Language.English, export.SelectLanguage.Value == Language.English));
     Languages.Add(new NamedIntViewModel(translator, Language.German, export.SelectLanguage.Value == Language.German));
     Languages.Add(new NamedIntViewModel(translator, Language.French, export.SelectLanguage.Value == Language.French));
     Languages.Add(new NamedIntViewModel(translator, Language.Italian, export.SelectLanguage.Value == Language.Italian));
     Languages.Add(new NamedIntViewModel(translator.Get("Export.Edit.Field.SelectLanguage.None", "No selection in the select language field of the edit export page", "None"), false, export.SelectLanguage.Value == null));
     var columns = new ExportColumnManager(translator);
     Columns = new List <NamedStringViewModel>(columns.Columns.Select(c => new NamedStringViewModel(c.Id, c.Title, false)));
 }
コード例 #2
0
        private bool IsPermittedExport(Export export)
        {
            var columns = new ExportColumnManager(Translator);

            if (export.SelectFeed.Value == null)
            {
                return(columns
                       .ComputeRequiredAccess(export.ExportColumns.Value)
                       .All(pa => HasSystemWideAccess(pa, AccessRight.Read)));
            }
            else
            {
                return(columns
                       .ComputeRequiredAccess(export.ExportColumns.Value)
                       .All(pa => HasAccess(export.SelectFeed.Value, pa, AccessRight.Read)));
            }
        }
コード例 #3
0
        public ExportEdit()
        {
            this.RequiresAuthentication();

            Get["/export"] = parameters =>
            {
                if (HasAnyFeedAccess(PartAccess.Demography, AccessRight.Read))
                {
                    return(View["View/export.sshtml",
                                new ExportViewModel(Translator, CurrentSession)]);
                }
                return(AccessDenied());
            };
            Get["/export/list"] = parameters =>
            {
                if (HasAnyFeedAccess(PartAccess.Demography, AccessRight.Read))
                {
                    return(View["View/exportlist.sshtml",
                                new ExportListViewModel(Translator, Database)]);
                }
                return(null);
            };
            Get["/export/edit/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    export   = Database.Query <Export>(idString);

                if (export != null)
                {
                    if (IsPermittedExport(export))
                    {
                        return(View["View/exportedit.sshtml",
                                    new ExportEditViewModel(Translator, Database, CurrentSession, export)]);
                    }
                }
                return(null);
            };
            Post["/export/edit/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    model    = JsonConvert.DeserializeObject <ExportEditViewModel>(ReadBody());
                var    export   = Database.Query <Export>(idString);
                var    status   = CreateStatus();

                if (status.ObjectNotNull(export))
                {
                    status.AssignStringRequired("Name", export.Name, model.Name);
                    status.AssignObjectIdString("SelectFeed", export.SelectFeed, model.SelectFeed);
                    status.AssignObjectIdString("SelectTag", export.SelectTag, model.SelectTag);
                    status.AssignEnumIntString("SelectLanguage", export.SelectLanguage, model.SelectLanguage);
                    status.AssignStringList("ExportColumns", export.ExportColumns, model.ExportColumns);

                    if (status.IsSuccess)
                    {
                        if (IsPermittedExport(export))
                        {
                            Database.Save(export);
                            Notice("{0} changed export {1}", CurrentSession.User.UserName.Value, export);
                        }
                        else
                        {
                            status.SetErrorAccessDenied();
                        }
                    }
                }

                return(status.CreateJsonData());
            };
            Get["/export/add"] = parameters =>
            {
                if (HasAnyFeedAccess(PartAccess.Demography, AccessRight.Read))
                {
                    return(View["View/exportedit.sshtml",
                                new ExportEditViewModel(Translator, Database, CurrentSession)]);
                }
                return(null);
            };
            Post["/export/add/new"] = parameters =>
            {
                string idString = parameters.id;
                var    body     = ReadBody();
                var    model    = JsonConvert.DeserializeObject <ExportEditViewModel>(body);
                var    export   = new Export(Guid.NewGuid());
                var    status   = CreateStatus();
                status.AssignStringRequired("Name", export.Name, model.Name);
                status.AssignObjectIdString("SelectFeed", export.SelectFeed, model.SelectFeed);
                status.AssignObjectIdString("SelectTag", export.SelectTag, model.SelectTag);
                status.AssignEnumIntString("SelectLanguage", export.SelectLanguage, model.SelectLanguage);
                status.AssignStringList("ExportColumns", export.ExportColumns, model.ExportColumns);

                if (status.IsSuccess)
                {
                    if (IsPermittedExport(export))
                    {
                        Database.Save(export);
                        Notice("{0} added export {1}", CurrentSession.User.UserName.Value, export);
                    }
                    else
                    {
                        status.SetErrorAccessDenied();
                    }
                }

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

                if (status.ObjectNotNull(export))
                {
                    if (IsPermittedExport(export))
                    {
                        using (var transaction = Database.BeginTransaction())
                        {
                            export.Delete(Database);
                            transaction.Commit();
                            Notice("{0} deleted export {1}", CurrentSession.User.UserName.Value, export);
                        }
                    }
                    else
                    {
                        status.SetErrorAccessDenied();
                    }
                }

                return(status.CreateJsonData());
            };
            Get["/export/download/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    export   = Database.Query <Export>(idString);

                if (export != null)
                {
                    if (IsPermittedExport(export))
                    {
                        var manager    = new ExportColumnManager(Translator);
                        var stream     = new MemoryStream();
                        var textWriter = new StreamWriter(stream);
                        textWriter.WriteLine(manager.ConstructHeader(export.ExportColumns.Value));

                        foreach (var contact in Query(export))
                        {
                            textWriter.WriteLine(manager.ConstructRow(contact, export.ExportColumns.Value));
                        }

                        textWriter.Flush();
                        stream.Position = 0;

                        var response = new StreamResponse(() => stream, "test/csv");
                        Notice("{0} exported data", CurrentSession.User.UserName.Value);
                        return(response.AsAttachment("export.csv"));
                    }
                }

                return(null);
            };
        }