Author Topic: Flexible permission system  (Read 3036 times)

RTB uses svn, and CityRPG uses BitBucket. This uses BitBucket. Now I use BitBucket.

That's strange. Five days before I saw this topic I wrote some really messy incomplete "matrix permissions" thing.

Code: [Select]
if ( !isObject( "roleManagerSO" ) )
{
new scriptObject( "roleManagerSO" );
}

if ( !isObject( "permManagerSO" ) )
{
new scriptObject( "permManagerSO" )
{
count = 0;
};
}

function gameConnection::perm( %this, %id )
{
if ( !isObject( %so = "permManagerSO" ) )
{
return false;
}

if ( !%so.isPerm( %id ) )
{
return false;
}

%accept = false;
%adminLvl = ( %this.bl_id == getNumKeyID ? 3 : ( %this.isSuperAdmin ? 2 : ( %this.isAdmin ? 1 : 0 ) ) );

if ( %adminLvl >= 3 && %so.permHost[ %id ] )
{
return true;
}

if ( %adminLvl >= %so.permDefault[ %id ] )
{
%accept = true;
}

for ( %i = 0 ; %i < %this.permCount ; %i++ )
{
if ( %this.permIndex[ %i ] $= %id )
{
if ( %this.permValue[ %id ] )
{
%accept = true;
}
else
{
return false;
}
}
}

for ( %i = 0 ; %i < %this.roleCount ; %i++ )
{
%obj = getRoleObjByID( %this.roleIndex[ %i ] );

if ( %obj.hasPerm( %id ) )
{
if ( %obj.permValue[ %id ] )
{
%accept = true;
}
else
{
return false;
}
}
}

return %accept;
}

function gameConnection::hasPerm( %this, %id )
{
for ( %i = 0 ; %i < %this.permCount ; %i++ )
{
if ( %this.permIndex[ %i ].id $= %id )
{
return true;
}
}

return false;
}

function getRoleObjByID( %id )
{
if ( !isObject( %so = "roleManagerSO" ) )
{
return -1;
}

%count = %so.getCount();

for ( %i = 0 ; %i < %count ; %i++ )
{
%obj = %so.getObject( %i );

if ( %obj.id $= %id )
{
return %obj;
}
}

return -1;
}

function gameConnection::removeNullPerms( %this )
{
if ( !isObject( %so = "permManagerSO" ) )
{
error( "ERROR: gameConnection::removeNullPerms() - unable to find permManagerSO!" );
return false;
}

%count = %this.permCount;

for ( %i = 0 ; %i < %count ; %i++ )
{
%perm = %this.permIndex[ %i ];

if ( !%so.isPerm( %perm ) )
{
%this.removePerm( %perm );
%i--;
}
}
}

function gameConnection::removeNullRoles( %this )
{
if ( !isObject( %so = "roleManagerSO" ) )
{
error( "ERROR: gameConnection::removeNullRoles() - unable to find roleManagerSO!" );
return false;
}

%count = %this.roleCount;

for ( %i = 0 ; %i < %count ; %i++ )
{
%role = %this.roleIndex[ %i ];

if ( !%so.isRole( %role ) )
{
%this.removeRole( %role );
%i--;
}
}
}

function gameConnection::addRole( %this, %id )
{
if ( !isObject( %so = "roleManagerSO" ) )
{
error( "ERROR: gameConnection::addRole() - unable to find roleManagerSO!" );
return false;
}

if ( !%so.isRole( %id ) )
{
warn( "gameConnection::addRole() - role" SPC %id SPC "does not exist!" );
return false;
}

if ( %this.hasRole( %id ) )
{
warn( "gameConnection::addRole() - role" SPC %id SPC "is already present!" );
return false;
}

%this.roleIndex[ %this.roleCount ] = %id;
%this.roleCount++;
}

function gameConnection::removeRole( %this, %id )
{
if ( !isObject( %so = "roleManagerSO" ) )
{
error( "ERROR: gameConnection::removeRole() - unable to find roleManagerSO!" );
return false;
}

if ( !%this.hasRole( %id ) )
{
warn( "gameConnection::addRole() - role" SPC %id SPC "is not present!" );
return false;
}

%found = false;

for ( %i = 0 ; %i < %this.roleCount ; %i++ )
{
if ( %found )
{
%this.roleIndex[ %i ] = %this.roleIndex[ %i + 1 ];
}
else
{
if ( %this.roleIndex[ %i ] $= %id )
{
%found = true;
%i--;
continue;
}
}
}

%this.roleCount--;
return true;
}

function gameConnection::hasRole( %this, %id )
{
for ( %i = 0 ; %i < %this.roleCount ; %i++ )
{
if ( %this.roleIndex[ %i ] $= %id )
{
return true;
}
}

return false;
}

function gameConnection::setPerm( %this, %id, %value )
{
if ( !isObject( %so = "permManagerSO" ) )
{
error( "ERROR: gameConnection::setPerm() - unable to find permManagerSO!" );
return false;
}

if ( !strLen( %id ) )
{
warn( "gameConnection::setPerm( id, value ) - null is not a valid permission id!" );
return false;
}

if ( !%so.isPerm( %id ) )
{
warn( "gameConnection::setPerm() - perm" SPC %id SPC "does not exist!" );
return false;
}

if ( !%this.hasPerm( %id ) )
{
%this.permIndex[ %this.permCount ] = %id;
%this.permCount++;
}

%this.permValue[ %id ] = %value;
return true;
}

function gameConnection::removePerm( %this, %id )
{
if ( !isObject( %so = "permManagerSO" ) )
{
error( "ERROR: gameConnection::removePerm() - unable to find permManagerSO!" );
return false;
}

if ( !strLen( %id ) )
{
warn( "gameConnection::removePerm( id ) - null is not a valid permission id!" );
return false;
}

if ( !%this.hasPerm( %id ) )
{
warn( "gameConnection::removePerm() - perm" SPC %id SPC "has no value on this role!" );
return false;
}

%this.permValue[ %id ] = "";
%found = false;

for ( %i = 0 ; %i < %this.permCount ; %i++ )
{
if ( %found )
{
%this.permIndex[ %i ] = %this.permIndex[ %i + 1 ];
}
else
{
if ( %this.permIndex[ %i ] $= %id )
{
%found = true;
%i--;
continue;
}
}
}

%this.permCount--;
return true;
}

function permManagerSO::isPerm( %this, %id )
{
for ( %i = 0 ; %i < %this.count ; %i++ )
{
if ( %this.element[ %i ] $= %id )
{
return true;
}
}

return false;
}

function roleManagerSO::isRole( %this, %id )
{
%count = %this.getCount();

for ( %i = 0 ; %i < %count ; %i++ )
{
if ( %this.getObject( %i ).id $= %id )
{
return true;
}
}

return false;
}

function roleManagerSO::removeNullPerms( %this )
{
if ( !isObject( %so = "permManagerSO" ) )
{
error( "ERROR: roleManagerSO::removeNullPerms() - unable to find permManagerSO!" );
return false;
}

%count = %this.getCount();

for ( %i = 0 ; %i < %count ; %i++ )
{
%role = %this.getObject( %i );

for ( %x = 0 ; %x < %x.count ; %x++ )
{
%perm = %role.element[ %i ];

if ( !%so.isPerm( %perm ) )
{
%role.removePerm( %perm );
%x--;
}
}
}
}

function roleSO::setPerm( %this, %id, %value )
{
if ( !isObject( %so = "permManagerSO" ) )
{
error( "ERROR: roleSO::setPerm() - unable to find permManagerSO!" );
return false;
}

if ( !strLen( %id ) )
{
warn( "roleSO::setPerm( id, value ) - null is not a valid permission id!" );
return false;
}

if ( !%so.isPerm( %id ) )
{
warn( "roleSO::setPerm() - perm" SPC %id SPC "does not exist!" );
return false;
}

if ( !%this.hasPerm( %id ) )
{
%this.element[ %this.count ] = %id;
%this.count++;
}

%this.permValue[ %id ] = %value;
return true;
}

function roleSO::removePerm( %this, %id )
{
if ( !isObject( %so = "permManagerSO" ) )
{
error( "ERROR: roleSO::removePerm() - unable to find permManagerSO!" );
return false;
}

if ( !strLen( %id ) )
{
warn( "roleSO::removePerm( id ) - null is not a valid permission id!" );
return false;
}

if ( !%this.hasPerm( %id ) )
{
warn( "roleSO::removePerm() - perm" SPC %id SPC "has no value on this role!" );
return false;
}

%this.permValue[ %id ] = "";
%found = false;

for ( %i = 0 ; %i < %this.count ; %i++ )
{
if ( %found )
{
%this.element[ %i ] = %this.element[ %i + 1 ];
}
else
{
if ( %this.element[ %i ] $= %id )
{
%found = true;
%i--;
continue;
}
}
}

%this.count--;
return true;
}

function roleSO::hasPerm( %this, %id )
{
for ( %i = 0 ; %i < %this.count ; %i++ )
{
if ( %this.element[ %id ] $= %id )
{
return true;
}
}

return false;
}

function createRole( %id, %name )
{
if ( !isObject( %so = "roleManagerSO" ) )
{
error( "ERROR: createRole() - unable to find roleManagerSO!" );
return false;
}

%obj = new scriptObject()
{
className = "roleSO";
parent = %so;

id = %id;
name = %name;
count = 0;
};

%obj.setName( "roleSO_" @ %id );
%so.add( %obj );

return %obj;
}

function deleteRole( %id )
{
if ( !isObject( %so = "roleManagerSO" ) )
{
error( "ERROR: deleteRole() - unable to find roleManagerSO!" );
return false;
}

if ( !%so.isRole( %id ) )
{
error( "ERROR: deleteRole() - role" SPC %id SPC "does not exist!" );
return false;
}

%count = %so.getCount();

for ( %i = 0 ; %i < %count ; %i++ )
{
%obj = %so.getObject( %i );

if ( %obj.id $= %id )
{
%obj.delete();
}
}

return true;
}

function registerPerm( %id, %name, %default, %host )
{
if ( !isObject( %so = "permManagerSO" ) )
{
error( "ERROR: registerPerm() - unable to find permManagerSO!" );
return false;
}

if ( !strLen( %default ) )
{
%default = 4;
}

if ( !strLen( %id ) )
{
warn( "registerPerm( id, name, default ) - null is not a valid permission id!" );
return false;
}

if ( %so.isPerm( %id ) )
{
warn( "registerPerm() - perm" SPC %id SPC "already exists, overwriting!" );
%so.permName[ %id ] = %name;
%so.permDefault[ %id ] = %default;
%so.permHost[ %id ] = %host;

return true;
}

%so.element[ %so.count ] = %id;
%so.permName[ %id ] = %name;
%so.permDefault[ %id ] = %default;
%so.permHost[ %id ] = %host;
%so.count++;

return true;
}

function unRegisterPerm( %id )
{
if ( !isObject( %so = "permManagerSO" ) )
{
error( "ERROR: registerPerm() - unable to find permManagerSO!" );
return false;
}

if ( !strLen( %id ) )
{
warn( "unRegisterPerm( id ) - null is not a valid permission id!" );
return false;
}

if ( !%so.isPerm( %id ) )
{
warn( "unRegisterPerm( id ) - permission '" @ %id @ "' does not exist!" );
return false;
}

%so.permName[ %id ] = "";
%so.permDefault[ %id ] = "";
%so.permHost[ %id ] = "";
%found = false;

for ( %i = 0 ; %i < %so.count ; %i++ )
{
if ( %found )
{
%so.element[ %i ] = %so.element[ %i + 1 ];
}
else
{
if ( %so.element[ %i ] $= %id )
{
%found = true;
%i--;
continue;
}
}
}

%so.count--;

if ( isObject( %so = "roleManagerSO" ) )
{
%so.removeNullPerms();
}

%count = clientGroup.getCount();

for ( %i = 0 ; %i < %count ; %i++ )
{
clientGroup.getObject( %i ).removeNullPerms();
}

return true;
}

registerPerm( "perms.role.create", "Create Role", 3, true );
registerPerm( "perms.role.assign", "Assign Permission to Role", 3, true );
registerPerm( "perms.role.revoke", "Revoke Permission from Role", 3, true );
registerPerm( "perms.role.delete", "Delete Role", 3, true );
registerPerm( "perms.perm.assign", "Assign Permission to Player", 3, true );
registerPerm( "perms.perm.revoke", "Revoke Permission from Player", 3, true );

RTB uses svn, and CityRPG uses BitBucket. This uses BitBucket. Now I use BitBucket.
I've pretty much always used BB for "major" add-ons though. We even used Mercurial for BlockBastion (the first commit in there is from 7th december 2010).


I have a stable version of the GUI now, you can get it from https://bitbucket.org/Jincux/server_permissions

You can probably just click on "get source" > "zip", and it will download as an add-on.

I have a stable version of the GUI now, you can get it from https://bitbucket.org/Jincux/server_permissions

You can probably just click on "get source" > "zip", and it will download as an add-on.
It isn't properly packaged though, you  need to repackage it yourself.