| 1 | package QBit::Application::Model::DBManager::Users; | ||
| 2 | $QBit::Application::Model::DBManager::Users::VERSION = '0.004'; | ||
| 3 | use qbit; | ||
| 4 | |||
| 5 | use base qw(QBit::Application::Model::DBManager); | ||
| 6 | |||
| 7 | use Data::Rmap ':all'; | ||
| 8 | |||
| 9 | __PACKAGE__->model_accessors(db => 'QBit::Application::Model::DB::Users'); | ||
| 10 | |||
| 11 | __PACKAGE__->register_rights( | ||
| 12 | [ | ||
| 13 | { | ||
| 14 | name => 'users', | ||
| 15 | description => sub {gettext('Rights for users')}, | ||
| 16 | rights => { | ||
| 17 | users_view => d_gettext('Right to view list of users'), | ||
| 18 | users_add => d_gettext('Right to add new user'), | ||
| 19 | users_edit => d_gettext('Right to edit user'), | ||
| 20 | }, | ||
| 21 | } | ||
| 22 | ] | ||
| 23 | ); | ||
| 24 | |||
| 25 | __PACKAGE__->model_fields( | ||
| 26 | id => {db => TRUE, pk => TRUE, default => TRUE, label => d_gettext('ID')}, | ||
| 27 | create_dt => {db => TRUE, label => d_gettext('Create date')}, | ||
| 28 | login => {db => TRUE, default => TRUE, label => d_gettext('Login')}, | ||
| 29 | mail => {db => TRUE, default => TRUE, label => d_gettext('Mail')}, | ||
| 30 | name => {db => TRUE, label => d_gettext('Name')}, | ||
| 31 | midname => {db => TRUE, label => d_gettext('Midname')}, | ||
| 32 | surname => {db => TRUE, label => d_gettext('Surname')}, | ||
| 33 | extra_fields => { | ||
| 34 | depends_on => ['id'], | ||
| 35 | label => d_gettext('Extra fields'), | ||
| 36 | get => sub { | ||
| 37 | $_[0]->{'__EXTRA_FIELDS__'}{$_[1]->{'id'}} // {}; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | ); | ||
| 41 | |||
| 42 | __PACKAGE__->model_filter( | ||
| 43 | db_accessor => 'db', | ||
| 44 | fields => { | ||
| 45 | id => {type => 'number'}, | ||
| 46 | create_dt => {type => 'text'}, | ||
| 47 | login => {type => 'text'}, | ||
| 48 | mail => {type => 'text'}, | ||
| 49 | name => {type => 'text'}, | ||
| 50 | midname => {type => 'text'}, | ||
| 51 | surname => {type => 'text'}, | ||
| 52 | } | ||
| 53 | ); | ||
| 54 | |||
| 55 | sub query { | ||
| 56 | my ($self, %opts) = @_; | ||
| 57 | |||
| 58 | my $filter = $self->db->filter($opts{'filter'}); | ||
| 59 | |||
| 60 | unless ($self->check_rights('users_view')) { | ||
| 61 | $filter->and(['id' => '=' => \$self->get_option('cur_user', {})->{'id'}]); | ||
| 62 | } | ||
| 63 | |||
| 64 | return $self->db->query->select( | ||
| 65 | table => $self->db->users, | ||
| 66 | fields => $opts{'fields'}->get_db_fields(), | ||
| 67 | filter => $filter, | ||
| 68 | ); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub pre_process_fields { | ||
| 72 | my ($self, $fields, $result) = @_; | ||
| 73 | |||
| 74 | my $user_ids = array_uniq(map {$_->{'id'}} @$result); | ||
| 75 | |||
| 76 | if ($fields->need('extra_fields')) { | ||
| 77 | $fields->{'__EXTRA_FIELDS__'} = {}; | ||
| 78 | |||
| 79 | my $extra_fields = $self->db->users_extra_fields->get_all( | ||
| 80 | fields => [qw(user_id key value is_json)], | ||
| 81 | filter => {user_id => $user_ids} | ||
| 82 | ); | ||
| 83 | |||
| 84 | foreach my $rec (@$extra_fields) { | ||
| 85 | push( | ||
| 86 | @{$fields->{'__EXTRA_FIELDS__'}{$rec->{'user_id'}}{$rec->{'key'}}}, | ||
| 87 | $rec->{'is_json'} ? from_json($rec->{'value'}) : $rec->{'value'} | ||
| 88 | ); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | sub add { | ||
| 94 | my ($self, %opts) = @_; | ||
| 95 | |||
| 96 | throw Exception::Denied gettext("You can't add user") unless $self->check_rights('users_add'); | ||
| 97 | |||
| 98 | $self->_trim(\%opts); | ||
| 99 | |||
| 100 | $self->check_user(\%opts); | ||
| 101 | |||
| 102 | $opts{'create_dt'} //= curdate(oformat => 'db_time'); | ||
| 103 | |||
| 104 | my $extra_fields = delete($opts{'extra_fields'}); | ||
| 105 | |||
| 106 | my $id; | ||
| 107 | $self->db->transaction( | ||
| 108 | sub { | ||
| 109 | $id = $self->db->users->add(\%opts); | ||
| 110 | |||
| 111 | if (defined($extra_fields)) { | ||
| 112 | $extra_fields = $self->normalization_extra_fields($extra_fields); | ||
| 113 | |||
| 114 | my @data = (); | ||
| 115 | foreach my $key (keys(%$extra_fields)) { | ||
| 116 | push(@data, | ||
| 117 | map {{user_id => $id, key => $key, value => $_->{'value'}, is_json => $_->{'is_json'}}} | ||
| 118 | @{$extra_fields->{$key}}); | ||
| 119 | } | ||
| 120 | |||
| 121 | $self->db->users_extra_fields->add_multi(\@data) if @data; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | ); | ||
| 125 | |||
| 126 | return $id; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub _trim { | ||
| 130 | my ($self, $opts) = @_; | ||
| 131 | |||
| 132 | rmap_all {$_ =~ s/^\s+|\s+$//g if defined($_) && !ref($_)} $opts; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub normalization_extra_fields { | ||
| 136 | my ($self, $extra_fields) = @_; | ||
| 137 | |||
| 138 | throw Exception::BadArguments gettext('Expected hash') unless ref($extra_fields) eq 'HASH'; | ||
| 139 | |||
| 140 | rmap_to( | ||
| 141 | sub { | ||
| 142 | unless (in_array(ref($_), ['', 'ARRAY', 'HASH'])) { | ||
| 143 | die gettext('Option "extra_fields" must not contain something other than a array, scalar or hash'); | ||
| 144 | } | ||
| 145 | }, | ||
| 146 | ALL | CODE, | ||
| 147 | $extra_fields | ||
| 148 | ); | ||
| 149 | |||
| 150 | foreach my $key (keys(%$extra_fields)) { | ||
| 151 | if (ref($extra_fields->{$key}) eq 'HASH') { | ||
| 152 | $extra_fields->{$key} = [ | ||
| 153 | { | ||
| 154 | value => to_json($extra_fields->{$key}), | ||
| 155 | is_json => 1 | ||
| 156 | } | ||
| 157 | ]; | ||
| 158 | } elsif (ref($extra_fields->{$key}) eq 'ARRAY') { | ||
| 159 | my @norm_data = (); | ||
| 160 | |||
| 161 | foreach my $value (@{$extra_fields->{$key}}) { | ||
| 162 | if (ref($value)) { | ||
| 163 | push( | ||
| 164 | @norm_data, | ||
| 165 | { | ||
| 166 | value => to_json($value), | ||
| 167 | is_json => 1 | ||
| 168 | } | ||
| 169 | ); | ||
| 170 | } else { | ||
| 171 | push( | ||
| 172 | @norm_data, | ||
| 173 | { | ||
| 174 | value => $value, | ||
| 175 | is_json => 0 | ||
| 176 | } | ||
| 177 | ); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | $extra_fields->{$key} = \@norm_data; | ||
| 182 | } else { | ||
| 183 | if (defined($extra_fields->{$key})) { | ||
| 184 | $extra_fields->{$key} = [ | ||
| 185 | { | ||
| 186 | value => $extra_fields->{$key}, | ||
| 187 | is_json => 0 | ||
| 188 | } | ||
| 189 | ]; | ||
| 190 | } else { | ||
| 191 | $extra_fields->{$key} = []; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | return $extra_fields; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub edit { | ||
| 200 | my ($self, $id, %opts) = @_; | ||
| 201 | |||
| 202 | throw Exception::Denied gettext("You can't edit user") unless $self->check_rights('users_edit'); | ||
| 203 | |||
| 204 | $self->_trim(\%opts); | ||
| 205 | |||
| 206 | $self->check_user(\%opts); | ||
| 207 | |||
| 208 | my $exists_extra_fields = exists($opts{'extra_fields'}); | ||
| 209 | my $extra_fields = delete($opts{'extra_fields'}); | ||
| 210 | |||
| 211 | $self->db->transaction( | ||
| 212 | sub { | ||
| 213 | $self->db->users->edit($id, \%opts) if %opts; | ||
| 214 | |||
| 215 | if (defined($extra_fields)) { | ||
| 216 | $extra_fields = $self->normalization_extra_fields($extra_fields); | ||
| 217 | |||
| 218 | $self->db->users_extra_fields->delete( | ||
| 219 | $self->db->filter({user_id => $id, key => [keys(%$extra_fields)]})) | ||
| 220 | if %$extra_fields; | ||
| 221 | |||
| 222 | my @data = (); | ||
| 223 | foreach my $key (keys(%$extra_fields)) { | ||
| 224 | push(@data, | ||
| 225 | map {{user_id => $id, key => $key, value => $_->{'value'}, is_json => $_->{'is_json'}}} | ||
| 226 | @{$extra_fields->{$key}}); | ||
| 227 | } | ||
| 228 | |||
| 229 | $self->db->users_extra_fields->add_multi(\@data) if @data; | ||
| 230 | } elsif ($exists_extra_fields) { | ||
| 231 | $self->db->users_extra_fields->delete($self->db->filter({user_id => $id})); | ||
| 232 | } | ||
| 233 | } | ||
| 234 | ); | ||
| 235 | } | ||
| 236 | |||
| 237 | sub check_user { } | ||
| 238 | |||
| 239 | TRUE; | ||
| 240 | |||
| 241 | __END__ |