Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

When I Create a new Category w/ custom permission, I want all users to have no View/Add, by default.

DoyceTDoyceT ✭✭✭
edited May 2014 in Vanilla 2.0 - 2.8

Is this possible?

Because of the way we're using the forums (student website, on which each student and faculty member has their own role with user-specific viewing permissions and literally 100+ subforums, half of which change every semester), it would be INCREDIBLY helpful from the admin side if a newly created Category showed up with no roles able to see the Category, by default, when I check "This category has custom permissions."

Just as good: if any newly created role didn't already have Add/View on every "custom permission" category, by default.

In my case, there are simply FAR more Categories each role can't see than those they can, so starting with everything off will save us tremendous time in the long run.

I'm on version 2.0.18.11.

«1

Comments

  • hgtonighthgtonight MVP
    edited May 2014

    Yes!

    You will have to modify your permissions table manually to achieve it. In the GDN_Permission table, their should be a record that has a RoleID of 0, JunctionTable of Category, JunctionColumn of PermissionCategoryID, and JunctionID of NULL. In a default install, it is PermissionID = 8.

    You need to modify that record by changing all the fields Vanilla.Discussions.View, Vanilla.Discussions.Add, and Vanilla.Comments.Add from 3 to 2.

    Once that is done, all new categories will have no view permissions by default.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • DoyceTDoyceT ✭✭✭
    edited May 2014

    PERFECT.

    Just exactly what we needed - it even makes new Roles "blank" by default. Thank you!

  • DoyceTDoyceT ✭✭✭

    Out of curiosity, is there anything that would make the values in these three fields revert back to the default of 3, after I changed them?

    I made these changes, and it worked perfectly, but when I made a new category today, everything was back to default, and I had to go back into the database and change these back again.

    I haven't done any updates, aside from adding the MyProfile plugin, so I can't think why these fields would have reset.

  • I can't think of anything off the top of my head. The utility structure function might revert your changes.

    I can look into it when I get to my desk.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • DoyceTDoyceT ✭✭✭

    Thanks. I appreciate the check. It's not a problem, but I'd like to understand what's going on. :)

  • utlity/update and utility/structure will indeed change things back to the way they were before.
    (which is a good thing for most users, but a bad thing for you.)

    you will probably need to modify the core to change behavior.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • DoyceTDoyceT ✭✭✭

    Hmm. Rather not do that.

    How often do update and structure make those sorts of changes.

  • peregrineperegrine MVP
    edited May 2014

    How often do update and structure make those sorts of changes.

    every time you run them manually which you should when updating vanilla at the very least.

    perhaps this is the only time they globally reset permissions, haven't tested or checked. the best way "You" can find out is keep track of what possible changes you do, which cause them to revert to default settings.

    its simple enough. just change it back if you see it changed back to default.

    or create a little plugin that you can run that updates the values in the permission table and run it prior to creating custom permissions.

    or run your own sql script

    hgtonight may have other factoids for you.

    this is where the magic happens - where it gets set to 3

    I wouldn't change the core either (never a good idea to change core), but just for informational purposes.

    public function Define($PermissionNames, $Type = 'tinyint', $JunctionTable = NULL, $JunctionColumn = NULL) {
            $PermissionNames = (array)$PermissionNames;
    
          $Structure = $this->Database->Structure();
          $Structure->Table('Permission');
          $DefaultPermissions = array();
    
          $NewColumns = array();
    
          foreach($PermissionNames as $Key => $Value) {
                if(is_numeric($Key)) {
                    $PermissionName = $Value;
                $DefaultPermissions[$PermissionName] = 2;
                } else {
                    $PermissionName = $Key;
    
                if ($Value === 0)
                   $DefaultPermissions[$PermissionName] = 2;
                elseif ($Value === 1)
                   $DefaultPermissions[$PermissionName] = 3;
    

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • DoyceTDoyceT ✭✭✭

    It totally makes sense that it would revert on an update to vanilla. I can't think what I've done recently that would be 'major' enough to trigger something like this, though. Bit of a mystery. As you say, it's easy enough to set them back when they revert. Thanks for the explanation, though.

  • hgtonighthgtonight MVP
    edited May 2014

    I still haven't made it to my desk to verify, but you could create a plugin that has a public structure method that sets the default permissions on a structure update.

    This would keep your changes in place without modifying the core.

    EDIT - I can verify @peregrine's findings. The default permissions are reset on a structure update.

    @DoyceT‌ A plugin named WhereDidMyDefaultCategoryPermissionsGo with the following method should do the trick:

    public function Structure() {
      $PermissionModel = Gdn::PermissionModel();
      $PermissionModel->Database = Gdn::Database();
      $PermissionModel->SQL = Gdn::Database()->SQL();
    
      $PermissionModel->Define( array(
          'Vanilla.Discussions.View' => 0,
          'Vanilla.Discussions.Add' => 0,
          'Vanilla.Comments.Add' => 0
        ),
        'tinyint',
        'Category',
        'PermissionCategoryID'
      );
    }
    

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • DoyceTDoyceT ✭✭✭

    Okay!

    I know pretty much nothing about how to make a plugin, but I guess it's time to learn something new! :)

    (Do I want to set those values to 0 or 2? Right now, I'm setting them to 2.)

  • DoyceTDoyceT ✭✭✭

    And since I haven't said it before, I'll say it now - this is really a great, informative, supportive community. You guys have literally made this project I'm working on possible.

  • You want to keep it at 0. The model will set it to 2 as default.

    Plugins are pretty easy to get started with. Check out this tutorial at the community wiki: http://vanillawiki.homebrewforums.net/index.php/Hello_World_Plugin

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • DoyceTDoyceT ✭✭✭

    So this is what I set up.

    <?php if (!defined('APPLICATION')) exit(); ?>
    
    <?php
    $PluginInfo['WhereDidMyDefaultCategoryPermissionsGo'] = array(
       'Name' => 'Where Did My Default Category Permissions Go',
       'Description' => 'This plugin sets all new categories and users to have no restricted access enabled when created',
       'Version' => '0.1',
       'Author' => "Doyce Testerman",
       'AuthorEmail' => 'doyce.testerman@gmail.com',
       'AuthorUrl' => 'http://nila.edu'
    );
    ?>
    
    <?php
    class WhereDidMyDefaultCategoryPermissionsGoPlugin extends Gdn_Plugin {
    
        public function Structure() {
        $PermissionModel = Gdn::PermissionModel();
        $PermissionModel->Database = Gdn::Database();
        $PermissionModel->SQL = Gdn::Database()->SQL();
        $PermissionModel->Define( array(
        'Vanilla.Discussions.View' => 0,
        'Vanilla.Discussions.Add' => 0,
        'Vanilla.Comments.Add' => 0
        ),
        'tinyint',
        'Category',
        'PermissionCategoryID'
        );
        }
    }
    ?>
    

    This is all in a folder /plugins/WhereDidMyDefaultCategoryPermissionsGo/ in the file class.wheredidmydefaultcategorypermissionsgoplugin.php.

    Saved that, found it in my list of plugins, enabled it.

    Everything looks good, though it doesn't seem to have changed the structure of the database.

    Pondered that a bit, reread the comments above, and ran index.php?p=/utility/structure

    Got a lovely little confirmation screen asking if I wanted to make the changes set up in the plugin.

    Told it to run...

    And it worked. Victory!

    Thanks so much, guys.

  • R_JR_J Admin

    Great you've found your way into Vanilla plugin development!

    Some comments: when a plugin is enabled, the function Setup() is called automagically. By now your plugin looks nice, but has no real functionality. It hooks into no events and there are no functions that are called when you plugin is enabled.

    Insert this into your plugin:

    public function Setup() {
       $this->Structure();
    }   
    

    After that simply enabling your plugin is enough to change db structure.

  • DoyceTDoyceT ✭✭✭

    @r_j Thanks! Would put this in somewhere around line 16?

  • R_JR_J Admin

    It must be somewhere inside of your class. Where you'll put it doesn't matter, but since it is executed first, I would simply place it as the first function in your class. So yes, below class WhereDidMyDefaultCategoryPermissionsGoPlugin... and above public function Structure() {

    Some tend to order their functions by alphabet but you're lucky and don't have to make this decision in that case ;)

  • @R_J said:
    By now your plugin looks nice, but has no real functionality. It hooks into no events and there are no functions that are called when you plugin is enabled.

    That is by design. The whole purpose of this plugin is to reapply the permission changes he wants after Vanilla reverts them on a structure update.

    That said, it is probably a good idea to run that in the setup function as you said. :D

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • R_JR_J Admin

    When does that happen? Would it make sense to hook some CategoryModel event(s)?

  • @R_J said:
    When does that happen? Would it make sense to hook some CategoryModel event(s)?

    What is 'that'?

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

Sign In or Register to comment.