Error when remove a role

Created on 23 January 2023, over 1 year ago
Updated 17 February 2023, over 1 year ago

Problem/Motivation

When I try to remove a role, I get this error

TypeError: array_search() expects parameter 2 to be array, null given in entity_share/modules/entity_share_server/src/Entity/Channel.php on line 184

The error is in function

public function removeAuthorizedRole($role) {
    $authorized_roles = $this->authorized_roles;
    $key = array_search($role, $authorized_roles);
    if ($key !== FALSE) {
      unset($authorized_roles[$key]);
      $this->set('authorized_roles', $authorized_roles);
      return TRUE;
    }

    return FALSE;
  }

This error occurs when $authorized_roles is empty, so this should be checked before array_search function.

🐛 Bug report
Status

Closed: cannot reproduce

Version

3.0

Component

Code

Created by

🇫🇷France lucuhb

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @lucuhb
  • 🇮🇳India _pratik_ Banglore

    Please try this patch.
    Thanks

  • Status changed to Needs review over 1 year ago
  • 🇫🇷France lucuhb

    That's Ok now, no more error when I remove a role. Thanks !

  • Assigned to Grimreaper
  • 🇫🇷France Grimreaper France 🇫🇷
  • Issue was unassigned.
  • Status changed to Postponed: needs info over 1 year ago
  • 🇫🇷France Grimreaper France 🇫🇷

    Hi,

    I can't reproduce the problem.

    $this->authorized_roles, as well as $this->authorized_users for the case of user deletion, are empty arrays if I have not set specific roles or users to access the channel. And empty arrays do not provoke errors.

    If you read the error message, it is written:

    expects parameter 2 to be array, null given

    So I guess that authorized_roles is not set at all in your channel configuration.

    This is coming from a change introduced in 8.x-3.0-rc2 #3211003: Add more options for channel access , which required to execute post_update hooks to ensure configuration is in a good state.

    Have you tried to resave your channels configuration before deleting your role?

  • Status changed to Closed: cannot reproduce over 1 year ago
  • 🇫🇷France Grimreaper France 🇫🇷

    Closing to clean up the issues queue.

    Feel free to re-open if able to provide steps to reproduce starting from a fresh standard Drupal install.

  • 🇫🇷France lucuhb

    Hello,

    Having no role configured in a channel should not cause an error when a role is removed.

  • 🇫🇷France Grimreaper France 🇫🇷

    Hi,

    Having no role configured in a channel should not cause an error when a role is removed.

    Absolutely, and this is not in opposition of what I had written in comment 6.

    So I guess that authorized_roles is not set at all in your channel configuration.

    This sentence does not mean you have no role configured in your channel, this means that I think the current config structure in your channels entity is not the expected one and you hould try to resave it and see if a difference appear.

Production build 0.69.0 2024