ShopOrder: billing_email zusätzlich als invoice_mail ...
[kivitendo-erp.git] / SL / DB / AuthGroup.pm
1 package SL::DB::AuthGroup;
2
3 use strict;
4
5 use SL::DB::MetaSetup::AuthGroup;
6 use SL::DB::Manager::AuthGroup;
7 use SL::DB::Helper::Util;
8
9 __PACKAGE__->meta->add_relationship(
10   users => {
11     type      => 'many to many',
12     map_class => 'SL::DB::AuthUserGroup',
13     map_from  => 'group',
14     map_to    => 'user',
15   },
16   rights => {
17     type       => 'one to many',
18     class      => 'SL::DB::AuthGroupRight',
19     column_map => { id => 'group_id' },
20   },
21   clients => {
22     type      => 'many to many',
23     map_class => 'SL::DB::AuthClientGroup',
24     map_from  => 'group',
25     map_to    => 'client',
26   },
27 );
28
29 __PACKAGE__->meta->initialize;
30
31 sub validate {
32   my ($self) = @_;
33
34   my @errors;
35   push @errors, $::locale->text('The name is missing.')    if !$self->name;
36   push @errors, $::locale->text('The name is not unique.') if !SL::DB::Helper::Util::is_unique($self, 'name');
37
38   return @errors;
39 }
40
41 sub get_employees {
42   my @logins = map { $_->login } $_[0]->users;
43   return @logins ? @{ SL::DB::Manager::Employee->get_all(query => [ login => \@logins ]) } : ();
44 }
45
46 sub rights_map {
47   my $self = shift;
48
49   if (@_) {
50     my %new_rights = ref($_[0]) eq 'HASH' ? %{ $_[0] } : @_;
51     $self->rights([ map { SL::DB::AuthGroupRight->new(right => $_, granted => $new_rights{$_} ? 1 : 0) } $::auth->all_rights ]);
52   }
53
54   return {
55     map({ ($_        => 0)           } $::auth->all_rights),
56     map({ ($_->right => $_->granted) } @{ $self->rights || [] })
57   };
58 }
59
60 1;
61 __END__
62
63 =pod
64
65 =encoding utf8
66
67 =head1 NAME
68
69 SL::DB::AuthGroup - RDBO model for auth.group
70
71 =head1 SYNOPSIS
72
73   # Outputting all rights granted to this group:
74   my $group  = SL::DB::Manager::AuthGroup->get_first;
75   my %rights = %{ $group->rights_map };
76   print "Granted rights:\n";
77   print "  $_\n" for sort grep { $rights{$_} } keys %rights;
78
79   # Set a right to 'yes':
80   $group->rights_map(%{ $group->rights_map }, invoice_edit => 1);
81   $group->save;
82
83 =head1 FUNCTIONS
84
85 =over 4
86
87 =item C<get_employees>
88
89 Returns all employees (as instances of L<SL::DB::Employee>) whose
90 corresponding logins are members in this group.
91
92 =item C<rights_map [$new_rights]>
93
94 Gets/sets the rights for this group as hashes. Returns a hash
95 references containing the right names as the keys and trueish/falsish
96 values for 'granted'/'not granted'.
97
98 If C<$new_rights> is given as a hash reference or a plain hash then it
99 will also set all rights from this hash.
100
101 =item C<validate>
102
103 Validates the object before saving (checks uniqueness, attribute
104 presence etc). Returns a list of human-readable errors and an empty
105 list on success.
106
107 =back
108
109 =head1 BUGS
110
111 Nothing here yet.
112
113 =head1 AUTHOR
114
115 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
116
117 =cut