Skip to content

Commit

Permalink
prepare using ShowDraftData
Browse files Browse the repository at this point in the history
  • Loading branch information
iJungleboy committed May 28, 2024
1 parent 9d90a7d commit 54f4323
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
6 changes: 4 additions & 2 deletions ToSic.Eav.Apps/Eav.Context/Internal/ContextOfApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ protected virtual IAppIdentity AppIdentity
private bool UserMayEdit => _userMayEditGet.Get(() => Log.GetterM(() =>
{
// Case 1: Superuser always may
if (User.IsSystemAdmin) return (true, "super");
if (User.IsSystemAdmin)
return (true, "super");

// Case 2: No App-State
if (AppState == null)
{
if (UserMayAdmin) return (true, "no app, use UserMayAdmin checks");
if (UserMayAdmin)
return (true, "no app, use UserMayAdmin checks");

// If user isn't allowed yet, it may be that the environment allows it
var fromEnv = AppServices.EnvironmentPermissions.New()
Expand Down
4 changes: 4 additions & 0 deletions ToSic.Eav.Core/Context/AdminPermissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ public AdminPermissions(bool both) : this(both, both) { }

public bool IsContentAdmin { get; } = isContentAdmin;

public bool IsContentEditor { get; } = isContentAdmin;

public bool IsSiteAdmin { get; } = isSiteAdmin;

public bool ShowDraftData => IsContentAdmin;
}
27 changes: 15 additions & 12 deletions ToSic.Eav.Core/Context/ContextOfUserPermissions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using ToSic.Eav.Plumbing;
using ToSic.Lib.Helpers;
using ToSic.Lib.Services;
using ToSic.Lib.Services;

namespace ToSic.Eav.Context;

Expand All @@ -10,20 +8,25 @@ namespace ToSic.Eav.Context;
/// </summary>
internal class ContextOfUserPermissions(IUser user) : ServiceBase("Eav.CtxSec"), IContextOfUserPermissions
{
public IUser User { get; } = user;
AdminPermissions IContextOfUserPermissions.Permissions => _permissions ??= GetPermissions();
private AdminPermissions _permissions;

private AdminPermissions GetPermissions()
{
var userIsSiteAdmin = UserMayAdmin();
var isContentAdmin = userIsSiteAdmin || (user?.IsContentAdmin ?? false);
return new(isContentAdmin, userIsSiteAdmin);
}

private bool UserMayAdmin()
{
var l = Log.Fn<bool>();
var u = User;
if (u == null) return l.Return(false, "user unknown, false");
// Case 1: Superuser always may
if (u.IsSystemAdmin) return l.Return(true, "super");

return l.Return(u.IsSiteAdmin || u.IsSiteDeveloper, "admin/developer");
if (user == null)
return l.Return(false, "user unknown, false");
return user.IsSystemAdmin
? l.Return(true, "super")
: l.Return(user.IsSiteAdmin || user.IsSiteDeveloper, "admin/developer");
}

AdminPermissions IContextOfUserPermissions.Permissions => _permissions ??= UserMayAdmin().Map(userMay => new AdminPermissions(userMay || (User?.IsContentAdmin ?? false), userMay));
private AdminPermissions _permissions;

}
13 changes: 3 additions & 10 deletions ToSic.Eav.DataSources/DataSource/Internal/Query/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,21 @@ public class QueryBuilder(
});


public QueryResult BuildQuery(QueryDefinition queryDef,
public QueryResult BuildQuery(
QueryDefinition queryDef,
ILookUpEngine lookUpEngineToClone,
List<ILookUp> overrideLookUps)
{
var l = Log.Fn<QueryResult>($"{queryDef.Title}({queryDef.Id}), hasLookUp:{lookUpEngineToClone != null}, overrides: {overrideLookUps?.Count}");
#region prepare shared / global value providers

var showDrafts = userPermissions.UserPermissions().IsContentAdmin;
var showDrafts = userPermissions.UserPermissions().ShowDraftData;
if (queryDef.ParamsLookUp is LookUpInDictionary paramsLookup)
paramsLookup.Properties[QueryConstants.ParamsShowDraftsKey] = showDrafts.ToString();

// centralizing building of the primary configuration template for each part
var baseLookUp = new LookUpEngine(lookUpEngineToClone, Log, sources: [queryDef.ParamsLookUp], overrides: overrideLookUps);

//baseLookUp.Add(queryDef.ParamsLookUp); // Add [params:...]
//baseLookUp.AddOverride(overrideLookUps); // add override

// 2023-03-13 2dm - #removedQueryPartShowDrafts - it's available on [Params:ShowDrafts] and I don't think every source needs it too
// provide global settings for ShowDrafts, ATM just if showdrafts are to be used
//var itemSettingsShowDrafts = new Dictionary<string, string>(InvariantCultureIgnoreCase)
// {{QueryConstants.ParamsShowDraftKey, showDrafts.ToString()}};

#endregion

#region Load Query Entity and Query Parts
Expand Down

0 comments on commit 54f4323

Please sign in to comment.