-
Notifications
You must be signed in to change notification settings - Fork 796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EntityGrid get current user/role #51
Comments
Add this class in a file next to your UserEndpoint.cs: namespace MyApplication
{
using Serenity;
using Serenity.ComponentModel;
using System;
using System.Collections.Generic;
[ScriptInclude]
public class UserData
{
public String Username { get; set; }
public Dictionary<string, bool> Permissions { get; set; }
}
} This method goes in UserEndpoint.cs to define a dynamic data script: [NonAction, DataScript("UserData", CacheDuration = -1)]
public UserData GetUserData()
{
var result = new UserData();
var user = Authorization.UserDefinition as UserDefinition;
if (user == null)
{
result.Permissions = new Dictionary<string, bool>();
return result;
}
result.Username = user.Username;
result.Permissions = TwoLevelCache.GetLocalStoreOnly("UserPermissions:" + user.Id, TimeSpan.Zero,
UserPermissionRow.Fields.GenerationKey, () =>
{
var permissions = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
foreach (var permission in new UserPermissionRepository().ListPermissionKeys().Entities)
if (Authorization.HasPermission(permission))
permissions[permission] = true;
return permissions;
});
return result;
} Build, transform templates and then place this class in MyApplication.Script namespace MyApplication
{
using Serenity;
using System;
public class Authorization
{
public static UserData UserDefinition { get { return Q.GetRemoteData<UserData>("UserData"); } }
public static bool HasPermission(string permissionKey)
{
return UserDefinition.Permissions[permissionKey];
}
}
} Now you can access user data / permissions from client side. Just don't trust this information as script side data can be modified by user anytime using developer console. Use it just for display purposes. Double check on server side. |
Thanks !!!! I needed to show whether or not an "AddEqualityFilter " based on the user's role in the method CreateToolbarExtensions in a EntityGrid. |
You're welcome |
hello, first of all congratulations for your work, is ti possibile retrieve the current user and roles int the project serene.script?
can you help me?
thank you
The text was updated successfully, but these errors were encountered: