Added admin check and moved tag class to separate file with serialization attributes for lowercasing property names.

This commit is contained in:
Jeff
2015-09-28 15:52:43 +01:00
parent 8adb69de54
commit c81fc70251
3 changed files with 42 additions and 18 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Web.Http; using System.Web.Http;
using Orchard.ContentManagement; using Orchard.ContentManagement;
@@ -9,25 +10,33 @@ using Orchard.Taxonomies.Helpers;
using Orchard.Taxonomies.Models; using Orchard.Taxonomies.Models;
using Orchard.Taxonomies.Services; using Orchard.Taxonomies.Services;
using System.Linq; using System.Linq;
using Orchard.Security;
using Orchard.Taxonomies.ViewModels;
namespace Orchard.Taxonomies.Controllers { namespace Orchard.Taxonomies.Controllers {
public class TagsController : ApiController { public class TagsController : ApiController {
private readonly ITaxonomyService _taxonomyService; private readonly ITaxonomyService _taxonomyService;
private readonly IContentManager _contentManager; private readonly IContentManager _contentManager;
private readonly IAuthorizer _authorizer;
public Localizer T { get; set; } public Localizer T { get; set; }
protected ILogger Logger { get; set; } protected ILogger Logger { get; set; }
public TagsController( public TagsController(
ITaxonomyService taxonomyService, ITaxonomyService taxonomyService,
IContentManager contentManager) { IContentManager contentManager,
IAuthorizer authorizer) {
_taxonomyService = taxonomyService; _taxonomyService = taxonomyService;
T = NullLocalizer.Instance; T = NullLocalizer.Instance;
_contentManager = contentManager; _contentManager = contentManager;
_authorizer = authorizer;
Logger = NullLogger.Instance; Logger = NullLogger.Instance;
} }
public IEnumerable<TagDto> Get(int taxonomyId, bool leavesOnly, string query) { public IEnumerable<Tag> Get(int taxonomyId, bool leavesOnly, string query) {
if (string.IsNullOrEmpty(query)) return new List<TagDto>(); if (!_authorizer.Authorize(StandardPermissions.AccessAdminPanel)) {
throw new UnauthorizedAccessException("Can't access the admin");
}
if (string.IsNullOrEmpty(query)) return new List<Tag>();
var allTerms = leavesOnly var allTerms = leavesOnly
? _taxonomyService.GetTerms(taxonomyId).ToList() ? _taxonomyService.GetTerms(taxonomyId).ToList()
: new List<TermPart>(); : new List<TermPart>();
@@ -36,25 +45,19 @@ namespace Orchard.Taxonomies.Controllers {
.Join<TitlePartRecord>() .Join<TitlePartRecord>()
.Where(r => r.Title.Contains(query)) .Where(r => r.Title.Contains(query))
.List() .List()
.Select(t => CreateTagDto(t, leavesOnly, allTerms)) .Select(t => BuildTag(t, leavesOnly, allTerms))
.OrderBy(t => t.label) .OrderBy(t => t.Label)
.ToList(); .ToList();
return matchingTerms; return matchingTerms;
} }
private static TagDto CreateTagDto(TermPart term, bool leavesOnly, IEnumerable<TermPart> terms) { private static Tag BuildTag(TermPart term, bool leavesOnly, IEnumerable<TermPart> terms) {
return new TagDto { return new Tag {
value = term.Id, Value = term.Id,
label = term.Name, Label = term.Name,
disabled = !term.Selectable || (leavesOnly && terms.Any(t => t.Path.Contains(term.Path + term.Id))), Disabled = !term.Selectable || (leavesOnly && terms.Any(t => t.Path.Contains(term.Path + term.Id))),
levels = term.GetLevels() Levels = term.GetLevels()
}; };
} }
} }
public class TagDto {
public string label { get; set; }
public int value { get; set; }
public int levels { get; set; }
public bool disabled { get; set; }
}
} }

View File

@@ -50,6 +50,9 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\..\..\lib\newtonsoft.json\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations"> <Reference Include="System.ComponentModel.DataAnnotations">
<RequiredTargetFramework>3.5</RequiredTargetFramework> <RequiredTargetFramework>3.5</RequiredTargetFramework>
@@ -76,6 +79,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="AdminMenu.cs" /> <Compile Include="AdminMenu.cs" />
<Compile Include="Controllers\AdminController.cs" /> <Compile Include="Controllers\AdminController.cs" />
<Compile Include="ViewModels\Tag.cs" />
<Compile Include="Controllers\TagsController.cs" /> <Compile Include="Controllers\TagsController.cs" />
<Compile Include="Controllers\TermAdminController.cs" /> <Compile Include="Controllers\TermAdminController.cs" />
<Compile Include="Drivers\TaxonomyNavigationPartDriver.cs" /> <Compile Include="Drivers\TaxonomyNavigationPartDriver.cs" />

View File

@@ -0,0 +1,17 @@
using Newtonsoft.Json;
namespace Orchard.Taxonomies.ViewModels {
public class Tag {
[JsonProperty("label")]
public string Label { get; set; }
[JsonProperty("value")]
public int Value { get; set; }
[JsonProperty("levels")]
public int Levels { get; set; }
[JsonProperty("disabled")]
public bool Disabled { get; set; }
}
}