Giter Site home page Giter Site logo

Comments (3)

volkanceylan avatar volkanceylan commented on May 17, 2024 1

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.

from serenity.

pierluigi-linardi avatar pierluigi-linardi commented on May 17, 2024

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

from serenity.

volkanceylan avatar volkanceylan commented on May 17, 2024

You're welcome

from serenity.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.