// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.Actions
{
///
/// The collection of actions
///
public class ActionCollection : BuilderCollectionBase
{
///
/// Initializes a new instance of the class.
///
public ActionCollection(Func> items)
: base(items)
{
}
///
/// Gets the action of the specified type.
///
/// The specified type to get
/// The action
public T? GetAction()
where T : IAction => this.OfType().FirstOrDefault();
///
/// Gets the actions by the specified letters
///
public IEnumerable GetByLetters(IEnumerable letters)
{
IAction[] actions = this.ToArray(); // no worry: internally, it's already an array
return letters
.Where(x => x.Length == 1)
.Select(x => actions.FirstOrDefault(y => y.Letter == x[0]))
.WhereNotNull()
.ToList();
}
///
/// Gets the actions from an EntityPermission
///
public IReadOnlyList FromEntityPermission(EntityPermission entityPermission)
{
IAction[] actions = this.ToArray(); // no worry: internally, it's already an array
return entityPermission.AssignedPermissions
.Where(x => x.Length == 1)
.SelectMany(x => actions.Where(y => y.Letter == x[0]))
.WhereNotNull()
.ToList();
}
}
}