| 1 | package Zabbix7::API::User; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use 5.010; | ||
| 6 | use Carp; | ||
| 7 | use autodie; | ||
| 8 | use utf8; | ||
| 9 | |||
| 10 | use Moo; | ||
| 11 | extends qw/Exporter Zabbix7::API::CRUDE/; | ||
| 12 | |||
| 13 | use constant { | ||
| 14 | USER_TYPE_USER => 1, | ||
| 15 | USER_TYPE_ADMIN => 2, | ||
| 16 | USER_TYPE_SUPERADMIN => 3, | ||
| 17 | }; | ||
| 18 | |||
| 19 | our @EXPORT_OK = qw/ | ||
| 20 | USER_TYPE_USER | ||
| 21 | USER_TYPE_ADMIN | ||
| 22 | USER_TYPE_SUPERADMIN | ||
| 23 | /; | ||
| 24 | |||
| 25 | our %EXPORT_TAGS = ( | ||
| 26 | user_types => [ | ||
| 27 | qw/USER_TYPE_USER | ||
| 28 | USER_TYPE_ADMIN | ||
| 29 | USER_TYPE_SUPERADMIN/ | ||
| 30 | ], | ||
| 31 | ); | ||
| 32 | |||
| 33 | sub _readonly_properties { | ||
| 34 | return { | ||
| 35 | userid => 1, | ||
| 36 | attempt_clock => 1, | ||
| 37 | attempt_failed => 1, | ||
| 38 | attempt_ip => 1, | ||
| 39 | roleid => 1, # Added for Zabbix 7.0 (read-only) | ||
| 40 | }; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub id { | ||
| 44 | my ($self, $value) = @_; | ||
| 45 | if (defined $value) { | ||
| 46 | $self->data->{userid} = $value; | ||
| 47 | Log::Any->get_logger->debug("Set userid: $value for user"); | ||
| 48 | return $self->data->{userid}; | ||
| 49 | } | ||
| 50 | my $id = $self->data->{userid}; | ||
| 51 | Log::Any->get_logger->debug("Retrieved userid for user: " . ($id // 'none')); | ||
| 52 | return $id; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _prefix { | ||
| 56 | my (undef, $suffix) = @_; | ||
| 57 | return 'user' . ($suffix // ''); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _extension { | ||
| 61 | return ( | ||
| 62 | output => 'extend', | ||
| 63 | selectUsrgrps => ['usrgrpid', 'name'], # Updated for Zabbix 7.0 | ||
| 64 | selectRole => ['roleid', 'name'], # Added for Zabbix 7.0 | ||
| 65 | ); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub name { | ||
| 69 | my $self = shift; | ||
| 70 | my $name = $self->data->{alias} || '???'; | ||
| 71 | Log::Any->get_logger->debug("Retrieved name for user ID: " . ($self->id // 'new') . ": $name"); | ||
| 72 | return $name; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _usergroup_or_name_to_usergroup { | ||
| 76 | my ($zabbix, $usergroup_or_name) = @_; | ||
| 77 | my $usergroup; | ||
| 78 | |||
| 79 | if (ref $usergroup_or_name and eval { $usergroup_or_name->isa('Zabbix7::API::UserGroup') }) { | ||
| 80 | $usergroup = $usergroup_or_name; | ||
| 81 | } elsif (not ref $usergroup_or_name) { | ||
| 82 | $usergroup = $zabbix->fetch_single('UserGroup', params => { filter => { name => $usergroup_or_name } }); | ||
| 83 | unless ($usergroup) { | ||
| 84 | Log::Any->get_logger->error("Usergroup '$usergroup_or_name' not found"); | ||
| 85 | croak "Parameter to add_to_usergroup or set_usergroups must be a Zabbix7::API::UserGroup object or an existing usergroup name"; | ||
| 86 | } | ||
| 87 | } else { | ||
| 88 | Log::Any->get_logger->error("Invalid usergroup parameter"); | ||
| 89 | croak "Parameter to add_to_usergroup or set_usergroups must be a Zabbix7::API::UserGroup object or an existing usergroup name"; | ||
| 90 | } | ||
| 91 | |||
| 92 | Log::Any->get_logger->debug("Resolved usergroup: " . $usergroup->name); | ||
| 93 | return $usergroup; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub add_to_usergroup { | ||
| 97 | my ($self, $usergroup_or_name) = @_; | ||
| 98 | croak('Cannot add user without ID to usergroup: needs to be created or fetched') | ||
| 99 | unless $self->id; | ||
| 100 | |||
| 101 | my $usergroup = _usergroup_or_name_to_usergroup($self->{root}, $usergroup_or_name); | ||
| 102 | $self->{root}->query( | ||
| 103 | method => 'usergroup.massadd', | ||
| 104 | params => { | ||
| 105 | usrgrpids => [ $usergroup->id ], | ||
| 106 | userids => [ $self->id ], | ||
| 107 | } | ||
| 108 | ); | ||
| 109 | Log::Any->get_logger->debug("Added user ID: " . $self->id . " to usergroup ID: " . $usergroup->id); | ||
| 110 | return $self; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub set_usergroups { | ||
| 114 | my ($self, @list_of_usergroups_or_names) = @_; | ||
| 115 | croak 'User does not exist (yet?) on server' | ||
| 116 | unless $self->id; # Replaced 'created' with 'id' check for consistency | ||
| 117 | |||
| 118 | my @list_of_usergroups = map { _usergroup_or_name_to_usergroup($self->{root}, $_) } @list_of_usergroups_or_names; | ||
| 119 | $self->{root}->query( | ||
| 120 | method => 'user.update', | ||
| 121 | params => { | ||
| 122 | userid => $self->id, | ||
| 123 | usrgrps => [ map { { usrgrpid => $_->id } } @list_of_usergroups ], # Zabbix 7.0 expects array of objects | ||
| 124 | } | ||
| 125 | ); | ||
| 126 | Log::Any->get_logger->debug("Set usergroups for user ID: " . $self->id . ", groups: " . join(', ', map { $_->name } @list_of_usergroups)); | ||
| 127 | return $self; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub set_password { | ||
| 131 | my ($self, $password) = @_; | ||
| 132 | $self->data->{passwd} = $password; | ||
| 133 | Log::Any->get_logger->debug("Set password for user ID: " . ($self->id // 'new')); | ||
| 134 | return $self; | ||
| 135 | } | ||
| 136 | |||
| 137 | before 'create' => sub { | ||
| 138 | my ($self) = @_; | ||
| 139 | delete $self->data->{userid}; # Ensure userid is not sent | ||
| 140 | delete $self->data->{roleid}; # roleid is read-only | ||
| 141 | Log::Any->get_logger->debug("Preparing to create user: " . ($self->data->{alias} // 'unknown')); | ||
| 142 | }; | ||
| 143 | |||
| 144 | before 'update' => sub { | ||
| 145 | my ($self) = @_; | ||
| 146 | delete $self->data->{roleid}; # roleid is read-only | ||
| 147 | Log::Any->get_logger->debug("Preparing to update user ID: " . ($self->id // 'new')); | ||
| 148 | }; | ||
| 149 | |||
| 150 | 1; | ||
| 151 | __END__ |