-
Notifications
You must be signed in to change notification settings - Fork 14
/
CustomContentApp.cs
96 lines (84 loc) · 3.13 KB
/
CustomContentApp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Routing;
using Umbraco.Extensions;
namespace DoStuff.Core
{
/// <summary>
/// some constants, just so we only change things in one place.
/// </summary>
/// <remarks>
/// you probibly would have a central constants class, with this
/// and other ones in so things are consistant across your whole
/// app.
/// </remarks>
internal class CustomContentAppConstants
{
// plugin base, doing this means it works when umbraco isn't installed
// in the root
private static string PluginBase = "/App_Plugins/DoStuff.ContentApp/";
internal const string Alias = "customContentApp";
internal const string Name = "CustomApp";
internal const string Icon = "icon-cloud";
internal static string AppView = PluginBase + "ContentApp.html";
}
/// <summary>
/// Composer to register our content app
/// </summary>
public class CustomContentAppComposer : IUserComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.ContentApps().Append<CustomContentApp>();
}
}
public class CustomContentApp : IContentAppFactory
{
private readonly string _contentAppView;
public CustomContentApp(UriUtility uriUtility)
{
_contentAppView = uriUtility.ToAbsolute(CustomContentAppConstants.AppView);
}
public ContentApp GetContentAppFor(object source, IEnumerable<IReadOnlyUserGroup> userGroups)
{
if (source is IContent content)
{
// this item a content item, our app works
// for content items
if (ShowApp(content, userGroups))
{
return new ContentApp()
{
Alias = CustomContentAppConstants.Alias,
Name = CustomContentAppConstants.Name,
Icon = CustomContentAppConstants.Icon,
View = _contentAppView
};
}
}
return null;
}
/// <summary>
/// Should we show the app to the user?
/// </summary>
/// <remarks>
/// one benefit of doing this in code vs the manifest is you
/// can run custom code to work out if the user should see the
/// app. here we are just checking groups, but you could do
/// anything (like check your own tables)
/// </remarks>
private bool ShowApp(IContent content, IEnumerable<IReadOnlyUserGroup> groups)
{
if (groups.Any(x => x.Alias.InvariantEquals("admin"))) return true;
// a permersion check ?
// this one is send to translate permission
// if (groups.Any(x => x.Permissions.Contains('5'))) return true;
return false;
}
}
}