2021-12-15 16:22:27 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2021-07-12 15:28:46 -06:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2017-08-14 18:21:48 +02:00
|
|
|
using System.Linq;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Composing;
|
|
|
|
|
using Umbraco.Cms.Core.Models.Membership;
|
|
|
|
|
using Umbraco.Extensions;
|
2017-08-14 18:21:48 +02:00
|
|
|
|
2021-02-18 11:06:02 +01: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>
|
2021-12-16 13:44:20 +01:00
|
|
|
public T? GetAction<T>()
|
2021-07-12 15:28:46 -06:00
|
|
|
where T : IAction => this.OfType<T>().FirstOrDefault();
|
|
|
|
|
|
2021-12-15 16:22:27 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the actions by the specified letters
|
|
|
|
|
/// </summary>
|
2020-06-09 07:49:26 +02:00
|
|
|
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
|
|
|
}
|
2018-10-29 17:27:33 +11:00
|
|
|
|
2021-12-15 16:22:27 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the actions from an EntityPermission
|
|
|
|
|
/// </summary>
|
2020-06-09 07:49:26 +02:00
|
|
|
public IReadOnlyList<IAction> FromEntityPermission(EntityPermission entityPermission)
|
2018-10-29 17:27:33 +11:00
|
|
|
{
|
2021-12-15 16:22:27 +01:00
|
|
|
IAction[] actions = this.ToArray(); // no worry: internally, it's already an array
|
2018-10-29 17:27:33 +11:00
|
|
|
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()
|
2018-10-29 17:27:33 +11:00
|
|
|
.ToList();
|
|
|
|
|
}
|
2017-08-14 18:21:48 +02:00
|
|
|
}
|
|
|
|
|
}
|