Automatically add users to a group on upon registration

Mitchell Pope Posted in Beginning Developers 7 years ago

Hey guys,

New to the platform and so far rather impressed with the setup!

Anyway as the title states; I am wondering if its possible to auto assign a new user to a designated group upon registration?

Thanks in Advance
Mitchell

Replies
Indonesian Arsalan Shah Replied 1 year ago

.

<?php
    function my_component_init(){
            ossn_register_callback('user', 'created', 'my_component_user_created_cb');
    }
    function my_component_user_created_cb($callback, $type, $variables){
            $guid = $variables['guid']; //contains the GUID of newly created user    
            $group_guid = 1234; //replace with actual group ID

            ossn_add_relation($guid, $group_guid, 'group:join');
            ossn_add_relation($group_guid, $guid, 'group:join:approve');      
    }
    ossn_register_callback('ossn', 'init', 'my_component_init');

.

Indonesian Arsalan Shah Replied 7 years ago

Hello Mitchell,

The quick was is if you see : https://github.com/opensource-socialnetwork/opensource-socialnetwork/blob/v5.x/components/OssnGroups/classes/OssnGroup.php#L48-L49

You can see that two add_relation function it actually add the person who creates group into group members.

To add some other user to group when it is created you add following lines below those lines:

ossn_add_relation(otheruserguid, $this->getGuid(), 'group:join');
ossn_add_relation($this->getGuid(), otheruserguid, 'group:join:approve');

Where otheruserguid is integer id of user like 1, 2, 22, 33,

Ok This method is not suggested when official because during next upgrade when you replace files the customization will be gone if you are developer and wanted to take some time you can do following:

  1. Create a new component.
  2. Override group add action.
  3. In group add action when group is created (if(group_reated) you can use those two new lines between it.