News:

Wondering if this will always be free?  See why free is better.

Main Menu

[SMF 2.0.2] How do I write new permissions?

Started by NanoSector, April 04, 2012, 10:37:06 AM

Previous topic - Next topic

NanoSector

Hi!

I've written a mod, which works with $user_info['is_admin'] and ['is_mod']. This sucks.

I wanted to make it work with permissions instead. I have no bloody idea how to write them though.

Someone cares to explain? :)
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Marcus Forsberg


NanoSector

Thanks a lot Marcus! I'll read the topic and see if I have any questions to it :)
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Suki

Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

NanoSector

Thanks, I've spotted that one too when looking through the code :P

Seems I'm finally trying to understand hooks (last mod of mine uses them). Might do everything with hooks leaving only essential file edits out.

Side-question --
Can I do "integrate_pre_load" or something AND have an action pointing to that same file?
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Suki

I'm not really get it,  pre_load just load a file everytime, everywhere, that is, your file will be available on any smf page, so yes, any function on that file can be used for an action with no issues.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

NanoSector

Nevermind, got it working. It was the integrate_include thing I was talking about.

Though I did NOT get the permission hook to work.

Using this code:

function RLEM_integrate_permissions(&$permissionList)
{
    // Modify!
    $temp = array('membergroup' => array('rlem_do' => array(true, 'post', 'modify', 'modify')));
   
    $permissionList = array_merge($permissionList, $temp);
   
    // Kindly return the modified variables for further "investigation" ;)
    return $permissionList;
}


It doesn't show up in the trash when I do a var_dump of it.

Any ideas?
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Suki

No, the permissions hook doesn't return anything, is jut a way to modify the existing permission arrays, for example:


/**
* Global permissions used by this mod per user group
*
* There is only permissions to post new status and comments on any profile because people needs to be able to post in their own profiles by default te same goes for deleting, people are able to delete their own status/comments on their own profile page.
* @param array $permissionGroups An array containing all possible permissions groups.
* @param array $permissionList An associative array with all the possible permissions.
* @return void
*/
public static function Permissions(&$permissionGroups, &$permissionList)
{
$permissionList['membergroup']['breeze_edit_settings_any'] = array(false, 'breeze_per_classic', 'breeze_per_simple');
$permissionGroups['membergroup']['simple'] = array('breeze_per_simple');
$permissionGroups['membergroup']['classic'] = array('breeze_per_classic');
$permissionList['membergroup']['breeze_deleteStatus'] = array(false, 'breeze_per_classic', 'breeze_per_simple');
$permissionList['membergroup']['breeze_postStatus'] = array(false, 'breeze_per_classic', 'breeze_per_simple');
$permissionList['membergroup']['breeze_postComments'] = array(false, 'breeze_per_classic', 'breeze_per_simple');
}


As you can see, here I created a new permission group and add it to that group my permissions for both the classic view and the Simple view, the text strings would be something like this:


/* Permissions strings */
$txt['cannot_view_general_wall'] = 'I\'m sorry, you are not allowed to view the Wall.';
$txt['permissiongroup_simple_breeze_per_simple'] = 'Breeze mod permissions';
$txt['permissiongroup_breeze_per_classic'] = 'Breeze mod permissions';
$txt['permissionname_breeze_deleteStatus'] = 'Delete all status/comments on any wall';
$txt['permissionname_breeze_postStatus'] = 'Post new Status on any wall';
$txt['permissionname_breeze_postComments'] = 'Post new Comments on any wall';
$txt['permissionname_breeze_edit_settings_any'] = 'Edit the user settings of any wall';
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

NanoSector

Wow, thanks a lot! That seems a bit bigger then I had it ;)

Going to try that in a moment :)
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Marcus Forsberg

The only reason it's bigger is because of the OOP structure and the fact that you add a single permission while Suki added a bunch of them. Not to mention all the commented lines above the function. In the end it's smaller than what you had. ;)

NanoSector

#10
Yeah, probably, heh.

Though when I add this:

function RLEM_integrate_permissions(&$permissionGroups, &$permissionList)
{
    // Modify!
    $permissionList['membergroup']['rlem_do'] = array(true, 'rlem_classic', 'rlem_simple', 'rlem_simple');
    $permissionGroups['membergroup']['simple'] = array('rlem_simple');
    $permissionGroups['membergroup']['classic'] = array('rlem_classic');
}


It shows up, but it shows up as 2x the same string. I want it to show something like "from their own post" and "from every post".

Sorry for sucking so much with permissions, lol.

Screenshot attached.
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Marcus Forsberg


NanoSector

This are all my text strings:

// Remove "Last edited" sign mod
$txt['remove_edit_sign'] = 'remove this sign';
$txt['remove_last_edited_error_1'] = 'Invalid number of resources in function UnsetEditTime, Subs-Unsetedittime.php';
$txt['remove_last_edited_error_2'] = 'You don\'t belong here, nifty little badass cracki...';
$txt['permissionname_rlem_do'] = 'Remove \'Last edit by ...\' from posts';
$txt['permissionname_rlem_do_own'] = 'Remove \'Last edit by ...\' from your own posts';
$txt['permissionname_rlem_do_any'] = 'Remove \'Last edit by ...\' from any post';
$txt['permissiongroup_simple_rlem_simple'] = 'Remove Last Edit Mod permissions';
$txt['permissiongroup_rlem_classic'] = 'Remove Last Edit Mod permissions';
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

NanoSector

You know, I dumped that. I don't think it's worth it.

Lets test without that multi-select thingy stuff.
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Suki

Multi select? where?

Adding a permission is simple, if you just want to add a permission without actually adding a permission group then just append your permission to an already existing permission group, the permission hook allows that since all you are doing is manipulating that array.

To try and fail is a goog method to learn, if you just quit then you will never learn.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

NanoSector

Quote from: Suki on April 04, 2012, 01:10:18 PM
Multi select? where?
Gosh, I'm bad with words today :P
I mean that you have the option to select two options instead of one.

Quote
Adding a permission is simple, if you just want to add a permission without actually adding a permission group then just append your permission to an already existing permission group, the permission hook allows that since all you are doing is manipulating that array.

Quote
To try and fail is a goog method to learn, if you just quit then you will never learn.
Yeah, you've got a point, but then I had to see if that post was the one posted by the user.
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Suki

Quote from: Yoshi2889 on April 04, 2012, 01:21:02 PM
Yeah, you've got a point, but then I had to see if that post was the one posted by the user.

Which post?

I believe the is a context value for that, context['user']['author'] or something like that, do a print_r on Display.php  or its template file to see all the context vars.

then you can just do a check

if( $user_info['id'] == $context['something'] && allowedto('some_permission'))

Quote from: Yoshi2889 on April 04, 2012, 01:21:02 PM
I mean that you have the option to select two options instead of one.

Which option?
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

NanoSector

#17
Quote from: Suki on April 04, 2012, 01:25:31 PM
Quote from: Yoshi2889 on April 04, 2012, 01:21:02 PM
Yeah, you've got a point, but then I had to see if that post was the one posted by the user.

Which post?

I believe the is a context value for that, context['user']['author'] or something like that, do a print_r on Display.php  or its template file to see all the context vars.

then you can just do a check

if( $user_info['id'] == $context['something'] && allowedto('some_permission'))
Seriously? Didn't even know such info was stored in $context :S

Will try that.

EDIT: Close, it was in $message ;)

Still, it functions now. Can I use allowedTo in templates, though?

Quote
Quote from: Yoshi2889 on April 04, 2012, 01:21:02 PM
I mean that you have the option to select two options instead of one.

Which option?

With some permissions you have two options:
- Own ...
- Any ...

With my code which should have two separate messages, it shows one message twice as you see on that screenshot.
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Suki

That option is usually used for profile permissions, means  own profile/ any profile.

You can have two separate permissions and work with both, if the user is the owner of that post then there is no need to check for the "any" permission.

It's better to assign a context var the permission in the Source function and then use that context var in your template.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

NanoSector

Quote from: Suki on April 04, 2012, 01:56:46 PM
That option is usually used for profile permissions, means  own profile/ any profile.

You can have two separate permissions and work with both, if the user is the owner of that post then there is no need to check for the "any" permission.

It's better to assign a context var the permission in the Source function and then use that context var in your template.
Okay, then I'll do that :)

Maybe I'll add it to the $message var.

Though I still don't understand why it's showing the same permission text twice...
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Advertisement: