Creating custom privilege



  • I want to create a new privilege for categories.
    I'm not quite sure how to go forward with this idea. What I've done so far is:

    var privileges = module.parent.require('./privileges');
    privileges.privilegeLabels.push({ name: '[[admin/manage/privileges:custom]]' });
    privileges.userPrivilegeList.push('topics:custom');
    

    Now what do I do? The above code creates a new column in the privileges table, but no clickable checkboxes.
    Where do I write the logic for the custom privilege?
    I want the plugin to filter topics in categories based on the privilege of users.
    I also find the source code very confusing, no JSDoc or any other comments.


  • Global Moderator

    You want to add your privilege by adding a hook handler for each of

    • filter:privileges.list
    • filter:privileges.groups.list
    • filter:privileges.list_human
    • filter:privileges.groups.list_human

    In the .list ones, you should have something like this:

    exports.privilegesList = function (list, callback) {
      list.push('your_privilege_id');
      callback(null, list);
    };
    

    and in the .list_human ones, you want to add a human-readable name or provide a translation token

    exports.privilegesListHuman = function (list, callback) {
      list.push('[[admin/your_plugin:your_privilege_id]]');
      callback(null, list);
    };
    

    All this does is add these privileges to the ACP interface for configuration. You can use the methods provided by the privileges core module to actually apply that permission, through methods like:

    • privileges.posts.can
    • privileges.topics.can
    • privileges.categories.filterUids
    • privileges.posts.filter

    You'd have to listen to other filter hooks in order to filter topics from the category listing and prevent users from viewing those topics via direct link.

    Here's an example of using a custom privileges from my plugin



  • @pitaj Thank you for the detailed answer, appreciate it!


Log in to reply
 

Suggested Topics

  • 4
  • 3
  • 3
  • 2
  • 1
| |