'*POST /api/libraries/{encoded_library_id}/permissions
Set permissions of the given library to the given role ids.
:param  encoded_library_id:      the encoded id of the library to set the permissions of
:type   encoded_library_id:      an encoded id string
:param   payload: dictionary structure containing:
:param  action:            (required) describes what action should be performed
available actions: remove_restrictions, set_permissions
:type   action:            str
:param  access_ids[]:      list of Role.id defining roles that should have access permission on the library
:type   access_ids[]:      string or list
:param  add_ids[]:         list of Role.id defining roles that should have add item permission on the library
:type   add_ids[]:         string or list
:param  manage_ids[]:      list of Role.id defining roles that should have manage permission on the library
:type   manage_ids[]:      string or list
:param  modify_ids[]:      list of Role.id defining roles that should have modify permission on the library
:type   modify_ids[]:      string or list
:type:      dictionary
:returns:   dict of current roles for all available permission types
:rtype:     dictionary
:raises: RequestParameterInvalidException, InsufficientPermissionsException, InternalServerError
RequestParameterMissingException'

@expose_api
def set_permissions(self, trans, encoded_library_id, payload=None, **kwd):

    if payload:
        kwd.update(payload)
    is_admin = trans.user_is_admin()
    current_user_roles = trans.get_current_user_roles()
    library = self.library_manager.get(trans, self.__decode_id(trans, encoded_library_id, 'library'))
    if (not (is_admin or trans.app.security_agent.can_manage_library_item(current_user_roles, library))):
        raise exceptions.InsufficientPermissionsException('You    do    not    have    proper    permission    to    modify    permissions    of    this    library.')
    new_access_roles_ids = util.listify(kwd.get('access_ids[]', None))
    new_add_roles_ids = util.listify(kwd.get('add_ids[]', None))
    new_manage_roles_ids = util.listify(kwd.get('manage_ids[]', None))
    new_modify_roles_ids = util.listify(kwd.get('modify_ids[]', None))
    action = kwd.get('action', None)
    if (action is None):
        if (payload is not None):
            return self.set_permissions_old(trans, library, payload, **kwd)
        else:
            raise exceptions.RequestParameterMissingException('The    mandatory    parameter    "action"    is    missing.')
    elif (action == 'remove_restrictions'):
        is_public = self.library_manager.make_public(trans, library)
        if (not is_public):
            raise exceptions.InternalServerError('An    error    occured    while    making    library    public.')
    elif (action == 'set_permissions'):
        valid_access_roles = []
        invalid_access_roles_names = []
        for role_id in new_access_roles_ids:
            role = self.role_manager.get(trans, self.__decode_id(trans, role_id, 'role'))
            (valid_roles, total_roles) = trans.app.security_agent.get_valid_roles(trans, library, is_library_access=True)
            if (role in valid_roles):
                valid_access_roles.append(role)
            else:
                invalid_access_roles_names.append(role_id)
        if (len(invalid_access_roles_names) > 0):
            log.warning(('The    following    roles    could    not    be    added    to    the    library    access    permission:    ' + str(invalid_access_roles_names)))
        valid_add_roles = []
        invalid_add_roles_names = []
        for role_id in new_add_roles_ids:
            role = self.role_manager.get(trans, self.__decode_id(trans, role_id, 'role'))
            (valid_roles, total_roles) = trans.app.security_agent.get_valid_roles(trans, library)
            if (role in valid_roles):
                valid_add_roles.append(role)
            else:
                invalid_add_roles_names.append(role_id)
        if (len(invalid_add_roles_names) > 0):
            log.warning(('The    following    roles    could    not    be    added    to    the    add    library    item    permission:    ' + str(invalid_add_roles_names)))
        valid_manage_roles = []
        invalid_manage_roles_names = []
        for role_id in new_manage_roles_ids:
            role = self.role_manager.get(trans, self.__decode_id(trans, role_id, 'role'))
            (valid_roles, total_roles) = trans.app.security_agent.get_valid_roles(trans, library)
            if (role in valid_roles):
                valid_manage_roles.append(role)
            else:
                invalid_manage_roles_names.append(role_id)
        if (len(invalid_manage_roles_names) > 0):
            log.warning(('The    following    roles    could    not    be    added    to    the    manage    library    permission:    ' + str(invalid_manage_roles_names)))
        valid_modify_roles = []
        invalid_modify_roles_names = []
        for role_id in new_modify_roles_ids:
            role = self.role_manager.get(trans, self.__decode_id(trans, role_id, 'role'))
            (valid_roles, total_roles) = trans.app.security_agent.get_valid_roles(trans, library)
            if (role in valid_roles):
                valid_modify_roles.append(role)
            else:
                invalid_modify_roles_names.append(role_id)
        if (len(invalid_modify_roles_names) > 0):
            log.warning(('The    following    roles    could    not    be    added    to    the    modify    library    permission:    ' + str(invalid_modify_roles_names)))
        permissions = {trans.app.security_agent.permitted_actions.LIBRARY_ACCESS: valid_access_roles}
        permissions.update({trans.app.security_agent.permitted_actions.LIBRARY_ADD: valid_add_roles})
        permissions.update({trans.app.security_agent.permitted_actions.LIBRARY_MANAGE: valid_manage_roles})
        permissions.update({trans.app.security_agent.permitted_actions.LIBRARY_MODIFY: valid_modify_roles})
        trans.app.security_agent.set_all_library_permissions(trans, library, permissions)
        trans.sa_session.refresh(library)
        trans.app.security_agent.copy_library_permissions(trans, library, library.root_folder)
    else:
        raise exceptions.RequestParameterInvalidException('The    mandatory    parameter    "action"    has    an    invalid    value.Allowed    values    are:    "remove_restrictions",    set_permissions"')
    roles = self.library_manager.get_current_roles(trans, library)
    return roles
