Files
Umbraco-CMS/src/Umbraco.Core/Actions/ActionCollection.cs

61 lines
2.0 KiB
C#
Raw Normal View History

2021-12-15 16:22:27 +01:00
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Collections.Generic;
2017-08-14 18:21:48 +02:00
using System.Linq;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Extensions;
2017-08-14 18:21:48 +02:00
namespace Umbraco.Cms.Core.Actions
2017-08-14 18:21:48 +02:00
{
2021-12-15 16:22:27 +01:00
/// <summary>
/// The collection of actions
/// </summary>
2017-08-14 18:21:48 +02:00
public class ActionCollection : BuilderCollectionBase<IAction>
{
2021-12-15 16:22:27 +01:00
/// <summary>
/// Initializes a new instance of the <see cref="ActionCollection"/> class.
/// </summary>
public ActionCollection(Func<IEnumerable<IAction>> items)
: base(items)
2017-08-14 18:21:48 +02:00
{
}
2017-09-15 18:22:19 +02:00
2021-12-15 16:22:27 +01:00
/// <summary>
/// Gets the action of the specified type.
/// </summary>
/// <typeparam name="T">The specified type to get</typeparam>
/// <returns>The action</returns>
public T? GetAction<T>()
where T : IAction => this.OfType<T>().FirstOrDefault();
2021-12-15 16:22:27 +01:00
/// <summary>
/// Gets the actions by the specified letters
/// </summary>
public IEnumerable<IAction> GetByLetters(IEnumerable<string> letters)
2017-09-15 18:22:19 +02:00
{
2021-12-15 16:22:27 +01:00
IAction[] actions = this.ToArray(); // no worry: internally, it's already an array
2018-11-26 14:43:19 +01:00
return letters
.Where(x => x.Length == 1)
.Select(x => actions.FirstOrDefault(y => y.Letter == x[0]))
2017-09-15 18:22:19 +02:00
.WhereNotNull()
2018-11-26 14:43:19 +01:00
.ToList();
2017-09-15 18:22:19 +02:00
}
2021-12-15 16:22:27 +01:00
/// <summary>
/// Gets the actions from an EntityPermission
/// </summary>
public IReadOnlyList<IAction> FromEntityPermission(EntityPermission entityPermission)
{
2021-12-15 16:22:27 +01:00
IAction[] actions = this.ToArray(); // no worry: internally, it's already an array
return entityPermission.AssignedPermissions
.Where(x => x.Length == 1)
2018-11-26 14:43:19 +01:00
.SelectMany(x => actions.Where(y => y.Letter == x[0]))
.WhereNotNull()
.ToList();
}
2017-08-14 18:21:48 +02:00
}
}