Allowing comments via user self registration plugin

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

Allowing comments via user self registration plugin

Post by Don Chambers »

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?
=Don=
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Allowing comments via user self registration plugin

Post by garvinhicking »

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/
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: Allowing comments via user self registration plugin

Post by Don Chambers »

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

EDIT: nevermind, I found it. :wink:
=Don=
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: Allowing comments via user self registration plugin

Post by Don Chambers »

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.
=Don=
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: Allowing comments via user self registration plugin

Post by Don Chambers »

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.
=Don=
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Allowing comments via user self registration plugin

Post by garvinhicking »

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/
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: Allowing comments via user self registration plugin

Post by Don Chambers »

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'
=Don=
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: Allowing comments via user self registration plugin

Post by Don Chambers »

Hold the phone.... shouldn't && $this->inGroup() be && !$this->inGroup() ??? ie, NOT in group?
=Don=
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: Allowing comments via user self registration plugin

Post by Don Chambers »

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?
=Don=
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Allowing comments via user self registration plugin

Post by garvinhicking »

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