pull/1425/head
tidusjar 8 years ago
parent fc3ed1105d
commit 7f2cfdafd1

@ -32,5 +32,7 @@ namespace Ombi.Core.Claims
public const string AutoApproveMovie = nameof(AutoApproveMovie);
public const string AutoApproveTv = nameof(AutoApproveTv);
public const string PowerUser = nameof(PowerUser);
public const string RequestTv = nameof(RequestTv);
public const string RequestMovie = nameof(RequestMovie);
}
}

@ -13,24 +13,27 @@ using Ombi.Store.Entities;
using Ombi.Helpers;
using Ombi.Notifications;
using Ombi.Notifications.Models;
using Ombi.Core.Rules;
namespace Ombi.Core.Engine
{
public class TvRequestEngine : BaseMediaEngine, ITvRequestEngine
{
public TvRequestEngine(ITvMazeApi tvApi, IRequestServiceMain requestService, IPrincipal user, INotificationService notificationService, IMapper map) : base(user, requestService)
public TvRequestEngine(ITvMazeApi tvApi, IRequestServiceMain requestService, IPrincipal user, INotificationService notificationService, IMapper map,
IRuleEvaluator rule) : base(user, requestService)
{
TvApi = tvApi;
NotificationService = notificationService;
Mapper = map;
Rules = rule;
}
private INotificationService NotificationService { get; }
private ITvMazeApi TvApi { get; }
private IMapper Mapper { get; }
private IRuleEvaluator Rules { get; }
public async Task<RequestEngineResult> RequestTvShow(SearchTvShowViewModel tv)
{
var showInfo = await TvApi.ShowLookupByTheTvDbId(tv.Id);
DateTime.TryParse(showInfo.premiered, out DateTime firstAir);
@ -68,7 +71,7 @@ namespace Ombi.Core.Engine
ImdbId = showInfo.externals?.imdb ?? string.Empty,
TvDbId = tv.Id.ToString(),
ProviderId = tv.Id,
};
model.ChildRequests.Add(childRequest);
@ -120,6 +123,15 @@ namespace Ombi.Core.Engine
}
var ruleResults = Rules.StartRequestRules(model);
if (ruleResults.Any(x => !x.Success))
{
return new RequestEngineResult()
{
ErrorMessage = ruleResults.FirstOrDefault(x => !string.IsNullOrEmpty(x.Message)).Message,
};
}
var existingRequest = await TvRequestService.CheckRequestAsync(model.Id);
if (existingRequest != null)
{
@ -146,7 +158,7 @@ namespace Ombi.Core.Engine
var allRequests = await TvRequestService.GetAllAsync();
var results = allRequests.FirstOrDefault(x => x.Id == request.Id);
results = Mapper.Map<TvRequestModel>(request);
var model = TvRequestService.UpdateRequest(results);
return model;
}

@ -7,11 +7,7 @@ using Ombi.Store.Entities;
namespace Ombi.Core.Models.Requests
{
public class BaseRequestModel : Entity
{
public BaseRequestModel()
{
RequestedUsers = new List<string>();
}
{
public int ProviderId { get; set; }
public string Overview { get; set; }
public string Title { get; set; }
@ -26,7 +22,7 @@ namespace Ombi.Core.Models.Requests
public IssueState Issues { get; set; }
public string OtherMessage { get; set; }
public string AdminNote { get; set; }
public List<string> RequestedUsers { get; set; }
public List<string> RequestedUsers { get; set; } = new List<string>();
public int IssueId { get; set; }
public bool Denied { get; set; }
public string DeniedReason { get; set; }

@ -0,0 +1,17 @@
using Ombi.Core.Rules;
namespace Ombi.Core.Rule
{
public abstract class BaseRule
{
public RuleResult Success()
{
return new RuleResult { Success = true };
}
public RuleResult Fail(string message)
{
return new RuleResult { Message = message };
}
}
}

@ -0,0 +1,9 @@
using Ombi.Core.Models.Requests;
namespace Ombi.Core.Rules
{
public interface IRequestRules<T> where T : BaseRequestModel
{
RuleResult Execute(T obj);
}
}

@ -0,0 +1,10 @@
using System.Collections.Generic;
using Ombi.Core.Models.Requests;
namespace Ombi.Core.Rules
{
public interface IRuleEvaluator
{
IEnumerable<RuleResult> StartRequestRules(BaseRequestModel obj);
}
}

@ -0,0 +1,43 @@
using Ombi.Core.Models.Requests;
using Ombi.Core.Rule;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Ombi.Core.Rules
{
public class RuleEvaluator : IRuleEvaluator
{
public RuleEvaluator(IServiceProvider provider)
{
RequestRules = new List<IRequestRules<BaseRequestModel>>();
var baseType = typeof(BaseRule).FullName;
System.Reflection.Assembly ass = typeof(RuleEvaluator).GetTypeInfo().Assembly;
foreach (TypeInfo ti in ass.DefinedTypes)
{
if (ti?.BaseType?.FullName == baseType)
{
var type = ti.GetType();
var item = Activator.CreateInstance(ti.GetType(), provider.GetService(type));// ti.GetType is wrong
RequestRules.Add((IRequestRules<BaseRequestModel>)item);
}
}
}
private List<IRequestRules<BaseRequestModel>> RequestRules { get; }
public IEnumerable<RuleResult> StartRequestRules(BaseRequestModel obj)
{
var results = new List<RuleResult>();
foreach (var rule in RequestRules)
{
var result = rule.Execute(obj);
results.Add(result);
}
return results;
}
}
}

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Ombi.Core.Rules
{
public class RuleResult
{
public bool Success { get; set; }
public string Message { get; set; }
}
}

@ -0,0 +1,36 @@
using Ombi.Core.Claims;
using Ombi.Core.Models.Requests;
using Ombi.Core.Rules;
using System.Security.Principal;
namespace Ombi.Core.Rule.Rules
{
public class CanRequestRule<T> : BaseRule where T : BaseRequestModel, IRequestRules<T>
{
public CanRequestRule(IPrincipal principal)
{
User = principal;
}
private IPrincipal User { get; }
public RuleResult Execute(T obj)
{
if (obj.Type == Store.Entities.RequestType.Movie)
{
if (User.IsInRole(OmbiClaims.RequestMovie))
{
return Success();
}
return Fail("You do not have permissions to Request a Movie");
}
else
{
if (User.IsInRole(OmbiClaims.RequestTv))
{
return Success();
}
return Fail("You do not have permissions to Request a Movie");
}
}
}
}

@ -21,6 +21,7 @@ using Ombi.Schedule.Jobs;
using Ombi.Settings.Settings;
using Ombi.Store.Context;
using Ombi.Store.Repository;
using Ombi.Core.Rules;
namespace Ombi.DependencyInjection
{
@ -43,6 +44,7 @@ namespace Ombi.DependencyInjection
services.AddTransient<IMovieRequestEngine, MovieRequestEngine>();
services.AddTransient<ITvRequestEngine, TvRequestEngine>();
services.AddTransient<ITvSearchEngine, TvSearchEngine>();
services.AddTransient<IRuleEvaluator, RuleEvaluator>();
}
public static void RegisterApi(this IServiceCollection services)

Loading…
Cancel
Save