Assistance Needed with User Permissions via Extension and Custom Login Event

Post here your questions about the HTML5 / JavaScript for SFS2X

Moderators: Lapo, Bax

Post Reply
ransaymour
Posts: 33
Joined: 20 Jun 2016, 18:30

Assistance Needed with User Permissions via Extension and Custom Login Event

Post by ransaymour »

Code: Select all

function onUserLogin(event) {
    var session = event.getParameter(SFSEventParam.SESSION);
    var username = event.getParameter(SFSEventParam.LOGIN_NAME);
    var password = event.getParameter(SFSEventParam.LOGIN_PASSWORD);
    var zone = getParentZone();
    var privilegeManager = zone.getPrivilegeManager();


    var privilegeProfile = privilegeManager.getPermissionProfile(2); // ID: Moderator

    trace("=== Starting Debugging ===");
    trace("Session: " + session);
    trace("Username: " + username);
    trace("Zone: " + zone);

    trace("Privilege profile object: " + privilegeProfile);
    trace("Privilege profile class: " + privilegeProfile.getClass().getName());
    trace("Privilege profile ID: " + privilegeProfile.getId());
    trace("Privilege profile name: " + privilegeProfile.getName());


    session.setProperty("$permission", privilegeProfile);

    trace("=== Debugging Complete ===");
}




Code: Select all

12:45:04,206 INFO  [SFSWorker:Ext:2] Extensions     - {pyTest}: Username: RanSaymour
12:45:04,206 INFO  [SFSWorker:Ext:2] Extensions     - {pyTest}: Zone: { Zone: SendAndLoad }
12:45:04,206 INFO  [SFSWorker:Ext:2] Extensions     - {pyTest}: Privilege profile object: com.smartfoxserver.v2.security.SFSPermissionProfile@1dab222e
12:45:04,209 INFO  [SFSWorker:Ext:2] Extensions     - {pyTest}: Privilege profile class: com.smartfoxserver.v2.security.SFSPermissionProfile
12:45:04,210 INFO  [SFSWorker:Ext:2] Extensions     - {pyTest}: Privilege profile ID: 2
12:45:04,210 INFO  [SFSWorker:Ext:2] Extensions     - {pyTest}: Privilege profile name: Moderator
12:45:04,211 INFO  [SFSWorker:Ext:2] Extensions     - {pyTest}: === Debugging Complete ===
12:45:04,213 WARN  [SFSWorker:Ext:2] managers.SFSExtensionManager     - java.lang.ClassCastException:
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Exception: java.lang.ClassCastException
Message: class com.smartfoxserver.v2.security.SFSPermissionProfile cannot be cast to class com.smartfoxserver.v2.security.IPermissionProfile (com.smartfoxserver.v2.security.SFSPermissionProfile and com.smartfoxserver.v2.security.IPermissionProfile are in unnamed module of loader 'app')
Description: Error during event handling: java.lang.ClassCastException: class com.smartfoxserver.v2.security.SFSPermissionProfile cannot be cast to class com.smartfoxserver.v2.security.IPermissionProfile (com.smartfoxserver.v2.security.SFSPermissionProfile and com.smartfoxserver.v2.security.IPermissionProfile are in unnamed module of loader 'app'), Listener: { Ext: pyTest, Type: JAVASCRIPT, Lev: ZONE, { Zone: SendAndLoad }, {} }
+--- --- ---+
Stack Trace:
+--- --- ---+
com.smartfoxserver.v2.api.SFSApi.login(SFSApi.java:520)
com.smartfoxserver.v2.controllers.system.Login.execute(Login.java:244)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.executeEventCommand(SFSExtensionManager.java:1047)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.dispatchEvent(SFSExtensionManager.java:781)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.dispatchZoneLevelEvent(SFSExtensionManager.java:694)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.handleServerEvent(SFSExtensionManager.java:898)
com.smartfoxserver.v2.core.SFSEventManager$SFSEventRunner.run(SFSEventManager.java:66)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
java.base/java.lang.Thread.run(Thread.java:829)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::



I am attempting to assign permissions to a user through an extension using the custom login event, but I keep encountering an error. No matter what I try, it indicates that the type of data I'm sending to set the user's permissions is incorrect. I would appreciate your help, as I am feeling quite frustrated and can't understand what's going wrong.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Assistance Needed with User Permissions via Extension and Custom Login Event

Post by Lapo »

Hi,
I recommend checking the documentation here:
https://docs2x.smartfoxserver.com/Advan ... ge-manager

As you can see permissions IDs are obtained from the DefaultPermissionProfile class
(see the example at the bottom of the article)

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
ransaymour
Posts: 33
Joined: 20 Jun 2016, 18:30

Re: Assistance Needed with User Permissions via Extension and Custom Login Event

Post by ransaymour »

Hi Lapo,
Thanks for the reply and the documentation link!
I realized that in a JavaScript extension, accessing `DefaultPermissionProfile` directly doesn't work because it’s not predefined by the system. However, this can be resolved by importing the class using Java integration. Here's how I managed to make it work:

Code: Select all

var DefaultPermissionProfile = Java.type('com.smartfoxserver.v2.security.DefaultPermissionProfile');


Once I imported the class this way, I was able to reference the profiles correctly, and the issue with casting the permission profile was resolved.
Hopefully, this clarification helps others encountering a similar issue when working with JavaScript extensions.
Thanks again for your assistance!
Post Reply