Moderator, Admin, Standard, etc.

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

dragagon
Posts: 19
Joined: 05 Dec 2010, 00:23

Post by dragagon »

I could see you do something slightly different that makes the code a little cleaner, but no less lengthy:

Code: Select all

IPermissionProfile profile = null;

switch(Short.parseShort((userData.getUtfString("PRIVILEGE")).trim()))
{
   case 1:
      profile = DefaultPermissionProfile.STANDARD;
      break;
   case 2:
      profile = DefaultPermissionProfile.MODERATOR;
      break;
   case 3:
      profile = DefaultPermissionProfile.ADMINISTRATOR;
      break;
   case 0:
   default:
      profile = DefaultPermissionProfile.GUEST;
}

session.setProperty($permission, profile);
It makes it a little easier to add new profile types in the future. I would also recommend that this be made its own function and placed in another class so that you can call it, pass it an int and have it return an IPermissionProfile so that you can call it from places and never repeat the code, but otherwise I don't see how you could optimize it further.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Actually there is a trick, but it's not implemented in this particular class.
A static method on the Enum itself which returns the proper IPermissionProfile instance from the numeric id.

It can be useful especially with Enums with lots of values. With this one it's probably not the case.

To exemplify:

Code: Select all

IPermissionProfile prof = DefaultPermissionProfile.fromId(theValue);
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Post by rjgtav »

When will it be implemented then?
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

As I explained we don't implement it for small enums like that. A normal if/then or switch can be used.
It was a suggestion for a custom implementation
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Post by rjgtav »

Oh ok sorry
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

We'll add a static fromId() method so that you can do this:

DefaultPermissionProfile profile = DefaultPermissionProfile profile =

Code: Select all

DefaultPermissionProfile.fromId(profileNmericValue);
switch(profile)
{
	case GUEST:
		// action for GUEST level user
	break;
	
	case STANDARD:
		// action for STANDARD level user
	break;
	
	case MODERATOR:
		// action for MODERATOR level user
	break;
}
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Post by rjgtav »

Thanks Lapo. Just what i needed :-)
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
Post Reply