Skip to content

Commit

Permalink
Make UserStatus::parse_roles public and static.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Apr 10, 2024
1 parent 1865f76 commit 9c978fb
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/userstatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,13 @@ private function make_keyed_object($x, $field, $lc = false) {
} else if (is_array($x)) {
foreach ($x as $v) {
if (!is_string($v)) {
$this->error_at($field, "<0>Format error [$field]");
$this->error_at($field, "<0>Format error [{$field}]");
} else if ($v !== "") {
$res[$lc ? strtolower($v) : $v] = true;
}
}
} else {
$this->error_at($field, "<0>Format error [$field]");
$this->error_at($field, "<0>Format error [{$field}]");
}
return (object) $res;
}
Expand Down Expand Up @@ -478,7 +478,7 @@ private function normalize($cj, $old_user) {
"affiliation", "phone", "new_password",
"city", "state", "zip", "country"] as $k) {
if (isset($cj->$k) && !is_string($cj->$k)) {
$this->error_at($k, "<0>Format error [$k]");
$this->error_at($k, "<0>Format error [{$k}]");
unset($cj->$k);
}
}
Expand Down Expand Up @@ -682,16 +682,17 @@ private function normalize_topics($cj, $old_user, $tk, $in_topics) {
}

/** @param int $old_roles
* @param ?MessageSet $ms
* @return int */
private function parse_roles($j, $old_roles) {
static function parse_roles($j, $old_roles, $ms = null) {
if (is_object($j) || is_associative_array($j)) {
$reset_roles = true;
$ij = [];
foreach ((array) $j as $k => $v) {
if ($v === true) {
$ij[] = $k;
} else if ($v !== false && $v !== null) {
$this->error_at("roles", "<0>Format error [roles]");
$ms && $ms->error_at("roles", "<0>Format error in roles");
return $old_roles;
}
}
Expand All @@ -703,15 +704,15 @@ private function parse_roles($j, $old_roles) {
$ij = $j;
} else {
if ($j !== null) {
$this->error_at("roles", "<0>Format error [roles]");
$ms && $ms->error_at("roles", "<0>Format error in roles");
}
return $old_roles;
}

$add_roles = $remove_roles = 0;
foreach ($ij as $v) {
if (!is_string($v)) {
$this->error_at("roles", "<0>Format error [roles]");
$ms && $ms->error_at("roles", "<0>Format error in roles");
return $old_roles;
} else if ($v !== "") {
$action = null;
Expand All @@ -720,13 +721,13 @@ private function parse_roles($j, $old_roles) {
$v = $m[2];
}
if ($v === "") {
$this->error_at("roles", "<0>Format error [roles]");
$ms && $ms->error_at("roles", "<0>Format error in roles");
return $old_roles;
} else if (is_bool($action) && strcasecmp($v, "none") === 0) {
$this->error_at("roles", "<0>Format error near “none” [roles]");
$ms && $ms->error_at("roles", "<0>Format error near “none”");
return $old_roles;
} else if (is_bool($reset_roles) && is_bool($action) === $reset_roles) {
$this->warning_at("roles", "<0>Expected ‘" . ($reset_roles ? "" : "+") . "{$v}’ in roles");
$ms && $ms->warning_at("roles", "<0>Expected ‘" . ($reset_roles ? "" : "+") . "{$v}’ in roles");
} else if ($reset_roles === null) {
$reset_roles = $action === null;
}
Expand All @@ -739,7 +740,7 @@ private function parse_roles($j, $old_roles) {
|| strcasecmp($v, "admin") === 0) {
$role = Contact::ROLE_ADMIN;
} else if (strcasecmp($v, "none") !== 0) {
$this->warning_at("roles", "<0>Unknown role ‘{$v}");
$ms && $ms->warning_at("roles", "<0>Unknown role ‘{$v}");
}
if ($action !== false) {
$add_roles |= $role;
Expand Down Expand Up @@ -913,7 +914,7 @@ function save_user($cj, $old_user = null) {
$this->normalize($cj, $user);
$roles = $old_roles = $old_user ? $old_user->roles : 0;
if (isset($cj->roles)) {
$roles = $this->parse_roles($cj->roles, $roles);
$roles = self::parse_roles($cj->roles, $roles, $this);
if ($old_user) {
$roles = $this->check_role_change($roles, $old_user);
}
Expand Down

0 comments on commit 9c978fb

Please sign in to comment.