eZ Community » Forums » Developer » Add a user to a group
expandshrink

Add a user to a group

Add a user to a group

Wednesday 24 February 2016 10:47:29 am - 3 replies

I am trying to assign a user to a group in addition to his existing groups.

$user = eZUser::fetchByEmail('myemail.com');

$user->attribute('groups'); // give me existing group array (2,4);

I want to add this user to another group, so $user->attribute('groups'); // will become (2,4,5);

Modified on Wednesday 24 February 2016 11:13:01 am by akhtar zaman

Wednesday 24 February 2016 12:35:51 pm

you must give the userNodeObject a second place or have to assign a userrole directly

i you want to add  a second place you can use the following php code

the following class is from our internal content/edithandler

 

But if you call JacMultiHandlerMultiLocation::multiLocation($parmaeters)

it should work from any php code inside ez.

You have to set the $parmeters array correctly  (contentobject ...) @see phpdoc

May be it will help to solve your problem

 

 
<?php
/**
 * File containing the JacMultiHandlerMultiLocation class
 *
 * @copyright Copyright (C) 2007-2011 CJW Network - Coolscreen.de, JAC Systeme GmbH, Webmanufaktur. All rights reserved.
 * @license http://ez.no/licenses/gnu_gpl GNU GPL v2
 * @package jacmultihandler
 * @filesource
 */
/**
 * This class implements the logic of the content edit handler function
 * multi_location
 * which allows you to create addition second place nodes for an $object
 *
 * ini example:
 *   # example to add a second location to for an object
 *   # here for all objects of contentclass  jac_article_news and article their will
 *   # be create a new location under node 2 and 43
 *   #
 *   # ClassIdentifierArray - the identifier the function will be matched
 *   # ProcessObjects - all|new which objectversion should be processed
 *   # Function - which function should be processed
 *   # ParameterArray - parameterarray which is paththrough the calle classMethod
 *
 *   #[setting_add_second_location_on_news]
 *   #ProcessObjects=all
 *   #ClassIdentifierArray[]
 *   #ClassIdentifierArray[]=jac_article_news
 *   #ClassIdentifierArray[]=article
 *   #Function=multi_location
 *   # variable parameter
 *   #ParameterArray[]
 *   #ParameterArray[second_place_node_ids]=2;43
 *   # only execute if current object is created under the defined node_id
 *   #ParameterArray[limit_to_node_ids]=212
 *
 * @package jacmultihandler
 */
class JacMultiHandlerMultiLocation
{

    function JacMultiHandlerMultiLocation()
    {
    }

    /*
       This function implements the logic of the handler function
       multi_location

       $parameter['contentobject_id']
       $parameter['contentobject_version']
       $parameter['contentobject']
       $parameter['parameter'] - the ini parameters 'ParameterArray'

     */
    static function multiLocation( $parameter )
    {
        $contentObject = $parameter['contentobject'];
        $customParameterArray = $parameter['parameter'];

        $secondPlaceNodeIdArr = array();

        if ( array_key_exists( 'second_place_node_ids', $customParameterArray ) )
            $secondPlaceNodeIdArr = explode( ';',
                                             $customParameterArray['second_place_node_ids'] );

        if ( array_key_exists( 'limit_to_node_ids', $customParameterArray ) )
            $limitToNodeIdArr = explode( ';',
                                         $customParameterArray['limit_to_node_ids'] );

        // only process if no node limitations are available
        if ( isset( $limitToNodeIdArr ) )
        {
            $assignedNodeIdArr = JacMultiHandlerMultiLocation::getAssignedParentNodeIds( $contentObject );

            $addLocations = false;

            foreach ( $limitToNodeIdArr as $parentNodeId )
            {
                if ( in_array( $parentNodeId, $assignedNodeIdArr ) )
                    $addLocations = true;
            }
        }
        else
        {
            $addLocations = true;
        }

        $resultArr = array();

        if ( $addLocations )
        {
            // add new locations
            foreach ( $secondPlaceNodeIdArr as $parentNodeId )
            {
                $resultArr[$parentNodeId] = JacMultiHandlerMultiLocation::addLocation( $contentObject,
                                                                                       $parentNodeId );
            }

            // update all assigned nodes  to set modified timestamp correctly
            $nodeArray = $contentObject->assignedNodes( );
            foreach ( $nodeArray as $node )
            {
                $node->store( );
            }
        }
        return $resultArr;
    }

    /**
     * only add a new location if it not already assigned
     *
     * @param eZContentObject $contentObject
     * @param int $newLocationNodeId new node_id for new locaiont
     * @return true if new location created
     *         false no new location is created
     */
    static function addLocation( $contentObject, $newLocationNodeId )
    {
        if ( is_object( $contentObject ) )
        {
            // check if nodeassignment already exists
            $assignedNodeIdArr = JacMultiHandlerMultiLocation::getAssignedParentNodeIds( $contentObject );
            // if not exists create new location
            if ( !in_array( $newLocationNodeId, $assignedNodeIdArr ) )
            {
                $newLocationNode = $contentObject->addLocation( $newLocationNodeId,
                                                                true );
                if ( is_object( $newLocationNode ) )
                {
                    // Now set it as published and fix main_node_id
                    $newLocationNode->setAttribute( 'contentobject_is_published',
                                                    1 );
                    $newLocationNode->setAttribute( 'main_node_id',
                                                    $contentObject->attribute( 'main_node_id' ) );
                    $newLocationNode->setAttribute( 'contentobject_version',
                                                    $contentObject->attribute( 'current_version' ) );
                    // Make sure the path_identification_string is set correctly.
                    $newLocationNode->updateSubTreePath( );
                    $newLocationNode->sync( );
                }
                return true;
            }
        }
        return false;
    }

    /**
     * @param eZContentObject $contentObject
     * @return an array of nodeIds which are assigned to ContentObject
     */
    static function getAssignedParentNodeIds( $contentObject )
    {
        $nodeIdArray = array();
        if ( is_object( $contentObject ) )
        {
            $nodeIdDataArray = $contentObject->assignedNodes( false );

            foreach ( $nodeIdDataArray as $nodeData )
            {
                $nodeIdArray[] = $nodeData['parent_node_id'];
            }
        }
        return $nodeIdArray;
    }

}
?>

Modified on Wednesday 24 February 2016 12:37:25 pm by Felix Woldt

Thursday 25 February 2016 10:12:47 am

Thanks Felix! It has been great help to me. Can you please also share some tips on how to remove user from a group? 

Thursday 25 February 2016 12:24:42 pm

To remove a location you can use the following function - this function we are using on an older 2012.06 project and it is marked as depricated.

But should work.

   eZContentOperationCollection::removeAssignment( $mainNodeId, $contentObjectID, $nodeRemoveList, false );
expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from