标签允许用户附加关键字说明以标识内容。ITaggableContentType 接口支持标签内容。
为什么我应该支持标签?
标签允许使用关键字关联内容,然后按关键字检索内容。
创建一个 ITaggableContentType 插件
添加对标签的支持可以通过实现 ITaggableContentType 接口来完成。它是在 Limyee.Core.dll 的 Limyee.Extensibility.Content.Version1 命名空间中定义。
请务必注意,可以在同一 IContentType 类中实现一个或多个核心服务。
首先检查用户是否能够添加标签。在此示例中,我们随意使用了“管理群组”权限,也可以通过您自己的权限替换。我们还会检查添加标签的用户是否与创建内容的用户是同一用户。这内容在平台中很常见,但由您来强制执行。
接下来,我们可以检查用户是否可以删除标签。在此示例中,我们使用相同的权限逻辑来检查删除标签的能力。
public bool CanAddTags(Guid contentId, int userId) { var content = LinksData.GetLink(contentId) as IContent; if (content == null) { return false; } var permission = Apis.Get<IPermissions>().Get(GroupPermission.ModifyGroup, userId, content.ContentId, content.ContentTypeId); return content.CreatedByUserId == userId || permission.IsAllowed; } public bool CanRemoveTags(Guid contentId, int userId) { var content = LinksData.GetLink(contentId) as IContent; if (content == null) { return false; } var permission = Apis.Get<IPermissions>().Get(GroupPermission.ModifyGroup, userId, content.ContentId, content.ContentTypeId); return content.CreatedByUserId == userId || permission.IsAllowed; }
下面是完整示例。
using System; using Limyee.Components; using Limyee.Extensibility; using Limyee.Extensibility.Api.Version1; using Limyee.Extensibility.Content.Version1; using IContent = Limyee.Extensibility.Content.Version1.IContent; namespace Samples.Links { public class LinkItemContentType : IContentType, ITaggableContentType { IContentStateChanges _contentState = null; #region IPlugin Members public string Description { get { return "Items in a Links collection"; } } public void Initialize() { } public string Name { get { return "Link Items"; } } #endregion #region IContentType Members public Guid[] ApplicationTypes { get { return new Guid[] { ContentTypes.LinksApplicationId }; } } public void AttachChangeEvents(IContentStateChanges stateChanges) { _contentState = stateChanges; } public Guid ContentTypeId { get { return ContentTypes.LinksItemId; } } public string ContentTypeName { get { return "Links Item"; } } public IContent Get(Guid contentId) { return LinksData.GetLink(contentId); } #endregion #region ITaggableContentType public bool CanAddTags(Guid contentId, int userId) { var content = LinksData.GetLink(contentId) as IContent; if (content == null) { return false; } var permission = Apis.Get<IPermissions>().Get(GroupPermission.ModifyGroup, userId, content.ContentId, content.ContentTypeId); return content.CreatedByUserId == userId || permission.IsAllowed; } public bool CanRemoveTags(Guid contentId, int userId) { var content = LinksData.GetLink(contentId) as IContent; if (content == null) { return false; } var permission = Apis.Get<IPermissions>().Get(GroupPermission.ModifyGroup, userId, content.ContentId, content.ContentTypeId); return content.CreatedByUserId == userId || permission.IsAllowed; } #endregion } }