Skip to content
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

Closed
pierluigi-linardi opened this issue Nov 21, 2015 · 3 comments
Closed

EntityGrid get current user/role #51

pierluigi-linardi opened this issue Nov 21, 2015 · 3 comments

Comments

@pierluigi-linardi
Copy link

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

@volkanceylan
Copy link
Member

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.

@pierluigi-linardi
Copy link
Author

Thanks !!!! I needed to show whether or not an "AddEqualityFilter " based on the user's role in the method CreateToolbarExtensions in a EntityGrid.
Thanks again

@volkanceylan
Copy link
Member

You're welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants