1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 
<?php

/* --------------------------------------------------------------
   AdminAccessRoleManager.inc.php 2018-01-15
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2016 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

/**
 * Class AdminAccessRoleManager
 *
 * @category   System
 * @package    AdminAccess
 * @subpackage Managers
 */
class AdminAccessRoleManager implements AdminAccessRoleManagerInterface
{
    /**
     * @var AdminAccessRoleFactoryInterface
     */
    protected $roleFactory;
    
    /**
     * @var AdminAccessRoleReaderInterface
     */
    protected $roleReader;
    
    
    /**
     * AdminAccessRoleManager constructor.
     *
     * @param AdminAccessRoleFactoryInterface $roleFactory Role factory.
     * @param AdminAccessRoleReaderInterface  $roleReader  Role reader.
     */
    public function __construct(AdminAccessRoleFactoryInterface $roleFactory,
                                AdminAccessRoleReaderInterface $roleReader)
    {
        $this->roleFactory = $roleFactory;
        $this->roleReader  = $roleReader;
    }
    
    
    /**
     * Returns a collection of all roles.
     *
     * @return AdminAccessRoleCollection Role collection with all available roles.
     */
    public function getAllRoles()
    {
        return $this->roleReader->getAll();
    }
    
    
    /**
     * Creates a new role.
     *
     * @param KeyValueCollection $name                    Collection with the role names. Index of a role name must be
     *                                                    his language code.
     * @param KeyValueCollection $description             Collection with the role descriptions. Index of a role name
     *                                                    must be his language code.
     * @param IntType            $sortOrder               Roles sort order.
     * @param BoolType           $unknownReadingGranted   Value of the reading permission for unknown groups.
     * @param BoolType           $unknownWritingGranted   Value of the writing permission for unknown groups.
     * @param BoolType           $unknownDeletingGranted  Value of the deleting permission for unknown groups.
     *
     * @return AdminAccessRoleInterface Returns the create role.
     */
    public function createNewRole(KeyValueCollection $name,
                                  KeyValueCollection $description,
                                  IntType $sortOrder,
                                  BoolType $unknownReadingGranted,
                                  BoolType $unknownWritingGranted,
                                  BoolType $unknownDeletingGranted)
    {
        // get new role object
        $role = $this->roleFactory->createAdminAccessRole();
        
        // set data for new role
        $role->setName($name);
        $role->setDescription($description);
        $role->setSortOrder($sortOrder);
        $role->setReadingUnknownGroupGranted($unknownReadingGranted);
        $role->setWritingUnknownGroupGranted($unknownWritingGranted);
        $role->setDeletingUnknownGroupGranted($unknownDeletingGranted);
        $role->store();
        
        return $role;
    }
    
    
    /**
     * Updates a role by a given role ID.
     *
     * @param IdType             $id                      Role ID to remove permission from.
     * @param KeyValueCollection $newName                 Collection with the new role names. Index of a role name must
     *                                                    be his language code.
     * @param KeyValueCollection $newDescription          Collection with the new role descriptions. Index of a role
     *                                                    name must be his language code.
     * @param IntType            $newSortOrder            New roles sort order.
     * @param BoolType           $unknownReadingGranted   Value of the reading permission for unknown groups.
     * @param BoolType           $unknownWritingGranted   Value of the writing permission for unknown groups.
     * @param BoolType           $unknownDeletingGranted  Value of the deleting permission for unknown groups.
     *
     * @return AdminAccessRoleManager Returns same instance for chained method calls.
     */
    public function updateRole(IdType $id,
                               KeyValueCollection $newName,
                               KeyValueCollection $newDescription,
                               IntType $newSortOrder,
                               BoolType $unknownReadingGranted,
                               BoolType $unknownWritingGranted,
                               BoolType $unknownDeletingGranted)
    {
        // fetch role by id
        $role = $this->roleReader->getById($id);
        
        // set new data for role
        $role->setName($newName);
        $role->setDescription($newDescription);
        $role->setSortOrder($newSortOrder);
        $role->setReadingUnknownGroupGranted($unknownReadingGranted);
        $role->setWritingUnknownGroupGranted($unknownWritingGranted);
        $role->setDeletingUnknownGroupGranted($unknownDeletingGranted);
        
        $role->update();
        
        return $this;
    }
    
    
    /**
     * Deletes role by a given role ID.
     *
     * @param IdType $id ID of the role that should be deleted.
     *
     * @return AdminAccessRoleManager Returns same instance for chained method calls.
     */
    public function deleteRole(IdType $id)
    {
        $role = $this->roleReader->getById($id);
        
        $role->delete();
        
        return $this;
    }
    
    
    /**
     * Returns a role by a given role ID.
     *
     * @param IdType $id ID of the requested role.
     *
     * @return AdminAccessRoleInterface
     */
    public function getRoleById(IdType $id)
    {
        return $this->roleReader->getById($id);
    }
}