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.Web.Http;
using Orchard.ContentManagement;
@@ -9,25 +10,33 @@ using Orchard.Taxonomies.Helpers;
using Orchard.Taxonomies.Models;
using Orchard.Taxonomies.Services;
using System.Linq;
using Orchard.Security;
using Orchard.Taxonomies.ViewModels;
namespace Orchard.Taxonomies.Controllers {
public class TagsController : ApiController {
private readonly ITaxonomyService _taxonomyService;
private readonly IContentManager _contentManager;
private readonly IAuthorizer _authorizer;
public Localizer T { get; set; }
protected ILogger Logger { get; set; }
public TagsController(
ITaxonomyService taxonomyService,
IContentManager contentManager) {
IContentManager contentManager,
IAuthorizer authorizer) {
_taxonomyService = taxonomyService;
T = NullLocalizer.Instance;
_contentManager = contentManager;
_authorizer = authorizer;
Logger = NullLogger.Instance;
}
public IEnumerable<TagDto> Get(int taxonomyId, bool leavesOnly, string query) {
if (string.IsNullOrEmpty(query)) return new List<TagDto>();
public IEnumerable<Tag> Get(int taxonomyId, bool leavesOnly, string query) {
if (!_authorizer.Authorize(StandardPermissions.AccessAdminPanel)) {
throw new UnauthorizedAccessException("Can't access the admin");
}
if (string.IsNullOrEmpty(query)) return new List<Tag>();
var allTerms = leavesOnly
? _taxonomyService.GetTerms(taxonomyId).ToList()
: new List<TermPart>();
@@ -36,25 +45,19 @@ namespace Orchard.Taxonomies.Controllers {
.Join<TitlePartRecord>()
.Where(r => r.Title.Contains(query))
.List()
.Select(t => CreateTagDto(t, leavesOnly, allTerms))
.OrderBy(t => t.label)
.Select(t => BuildTag(t, leavesOnly, allTerms))
.OrderBy(t => t.Label)
.ToList();
return matchingTerms;
}
private static TagDto CreateTagDto(TermPart term, bool leavesOnly, IEnumerable<TermPart> terms) {
return new TagDto {
value = term.Id,
label = term.Name,
disabled = !term.Selectable || (leavesOnly && terms.Any(t => t.Path.Contains(term.Path + term.Id))),
levels = term.GetLevels()
private static Tag BuildTag(TermPart term, bool leavesOnly, IEnumerable<TermPart> terms) {
return new Tag {
Value = term.Id,
Label = term.Name,
Disabled = !term.Selectable || (leavesOnly && terms.Any(t => t.Path.Contains(term.Path + term.Id))),
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>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\..\..\lib\newtonsoft.json\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
@@ -76,6 +79,7 @@
<ItemGroup>
<Compile Include="AdminMenu.cs" />
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="ViewModels\Tag.cs" />
<Compile Include="Controllers\TagsController.cs" />
<Compile Include="Controllers\TermAdminController.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; }
}
}