Allowing comments via user self registration plugin

Creating and modifying plugins.
Post Reply
User avatar
Don Chambers
Regular
Posts: 3638
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

Allowing comments via user self registration plugin

Post by Don Chambers » Tue Sep 18, 2012 4:38 pm

The user self registration plugin (serendipity_event_adduser) has an option that can limit comments to only registered users. Would it be possible to expand this functionality so that only specific user groups were allowed to comment?

User avatar
garvinhicking
Core Developer
Posts: 30020
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Allowing comments via user self registration plugin

Post by garvinhicking » Thu Sep 20, 2012 9:14 am

Hi Don!

Sure, should be possible. I just committed version 2.35 of the plugin ti git, can you check it out? I have no working test installation so I had to do this "blind"...

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/

User avatar
Don Chambers
Regular
Posts: 3638
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

Re: Allowing comments via user self registration plugin

Post by Don Chambers » Thu Sep 20, 2012 4:01 pm

Is there a github link or just wait for it to appear in spartacus?

EDIT: nevermind, I found it. :wink:

User avatar
Don Chambers
Regular
Posts: 3638
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

Re: Allowing comments via user self registration plugin

Post by Don Chambers » Thu Sep 20, 2012 4:54 pm

I installed the update... in the event configuration, there is still only an option labeled "Only registered users may post comments?"... there is no option to specify which user groups.

User avatar
Don Chambers
Regular
Posts: 3638
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

Re: Allowing comments via user self registration plugin

Post by Don Chambers » Thu Sep 20, 2012 5:17 pm

More info... I see where the new option was missing..... I changed this:

Code: Select all

        $propbag->add('configuration', array(
            'instructions',
            'registered_only',
            'true_identities'
        ));
to this:

Code: Select all

        $propbag->add('configuration', array(
            'instructions',
            'registered_only',
            'registered_only_group',
            'true_identities'
        ));
That now shows me the option where I can set specific user groups. However, it is not working.... users belonging to a group not selected in the new option can still comment.

User avatar
garvinhicking
Core Developer
Posts: 30020
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Allowing comments via user self registration plugin

Post by garvinhicking » Fri Sep 21, 2012 11:35 am

Hi!

Damn, sorry, forgot about that.

If it's not working, maybe we can add some debugging. Did you enable both options, registered_only AND registered_only_group? The group restriction will only work if registered_only is enabled as well.

As for debugging, you could modify the inGroup() function like this:

Code: Select all

 function inGroup() {
        global $serendipity;

        $checkgroups = explode('^', $this->get_config('registered_only_group'));

        $fp = fopen($serendipity['serendipityPath'] . '/templates_c/ingroup.txt', 'a');
        fwrite($fp, "InGroup check: " . print_r($checkgroups, true));

        // Not configured, so this shall not apply.
        if ($checkgroups[0] == '') {
            fwrite($fp, "InGroup not configured.");
            fclose($fp);

            return true;
        }

        if (!isset($serendipity['authorid']) || !is_array($checkgroups)) {
            fwrite($fp, "InGroup: Not logged in or empty array");
            fclose($fp);

            return false;
        }

        $mygroups =& serendipity_getGroups($serendipity['authorid'], true);
        fwrite($fp, "My groups:" . print_r($mygroups, true));

        if (!is_array($mygroups)) {
            fwrite($fp, "I have no groups");
            fclose($fp);

            return false;
        }

        foreach($checkgroups AS $key => $groupid) {
            if ($groupid == 'all') {
                fwrite($fp, "Allowed: Group key is 'all'");
                fclose($fp);

                return true;
            } elseif (in_array($groupid, $mygroups)) {
                fwrite($fp, "Allowed: Group key is '" . $groupid . "'");
                fclose($fp);

                return true;
            }
        }

        fwrite($fp, "Forbidden, no matches");
        fclose($fp);

        return false;
    }

This should give you a templates_c/ingroup.txt logfile that, everytime you comment, should get filled with metadata about why your comment is allowed...

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/

User avatar
Don Chambers
Regular
Posts: 3638
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

Re: Allowing comments via user self registration plugin

Post by Don Chambers » Fri Sep 21, 2012 3:59 pm

Thanks Garvin. Yes, the original option is checked allowing only registered users to comment. 4 out of 5 groups are marked as allowed to comment. I posted a comment by a user belonging to the group that should not be allowed to post comments, and the comment was allowed.

I then posted a comment by a user who should be allowed to comment, and that comment was rejected.

The result of your debugging is:

Code: Select all

InGroup check: Array
(
    [0] => 5
    [1] => 3
    [2] => 2
    [3] => 1
)
My groups:Array
(
    [0] => 4
)
Forbidden, no matches

InGroup check: Array
(
    [0] => 5
    [1] => 3
    [2] => 2
    [3] => 1
)
My groups:Array
(
    [0] => 3
)
Allowed: Group key is '3'

User avatar
Don Chambers
Regular
Posts: 3638
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

Re: Allowing comments via user self registration plugin

Post by Don Chambers » Fri Sep 21, 2012 5:00 pm

Hold the phone.... shouldn't && $this->inGroup() be && !$this->inGroup() ??? ie, NOT in group?

User avatar
Don Chambers
Regular
Posts: 3638
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

Re: Allowing comments via user self registration plugin

Post by Don Chambers » Fri Sep 21, 2012 5:29 pm

Don Chambers wrote:Hold the phone.... shouldn't && $this->inGroup() be && !$this->inGroup() ??? ie, NOT in group?
Confirmed. Works as expected. I even committed the changes to github.

As it works now, a logged in, registered user that does not belong to the authorized author group sees the comment form, but the comment is rejected when submitted. Would it be possible for these users to not see the comment form at all?

User avatar
garvinhicking
Core Developer
Posts: 30020
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Allowing comments via user self registration plugin

Post by garvinhicking » Mon Sep 24, 2012 3:07 pm

Hi!

Good catch! Thanks :-)
As it works now, a logged in, registered user that does not belong to the authorized author group sees the comment form, but the comment is rejected when submitted. Would it be possible for these users to not see the comment form at all?
Well....technically removing the comment form within a plugin has been "challenging" in the past; I think there were some issues with this that didn't make it so easy. I'll try to look into this.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/

Post Reply