1package Netdot::Model::GroupRight;
2
3use base 'Netdot::Model';
4use warnings;
5use strict;
6
7=head1 NAME
8
9Netdot::Model::GroupRight - Manipulate GroupRight objects
10
11=cut
12
13my $logger = Netdot->log->get_logger('Netdot::Model');
14
15=head1 CLASS METHODS
16=cut
17
18#########################################################################
19
20=head2 insert - Insert a new GroupRight object
21
22    We override the insert method for extra functionality
23    - Ignore duplicates
24    - Check if 'none' exists for given object and complain.
25    - If adding 'none' remove all other rights
26
27  Args:
28    userright table fields
29  Returns:
30    GroupRight object
31  Examples:
32    GroupRight->insert({contactlist=>$cl_id, accessright=>$ar_id });
33
34=cut
35
36sub insert {
37    my ($class, $argv) = @_;
38    $class->throw_fatal("Model::GroupRight::insert: Missing required arguments")
39	unless ( $argv->{contactlist} && $argv->{accessright} );
40
41    my $accessright = AccessRight->retrieve(int($argv->{accessright}));
42    my $cl          = ContactList->retrieve(int($argv->{contactlist}));
43
44    foreach my $r ( $cl->access_rights ){
45	my $ar = $r->accessright;
46	if ( $ar->object_class eq $accessright->object_class &&
47	     $ar->object_id eq $accessright->object_id ){
48	    # same object
49	    if ( $accessright->access eq $ar->access ){
50		# Do not try to insert rights if they exist
51		return $r;
52	    }
53	    if ( $accessright->access ne 'none' && $ar->access eq 'none' ){
54		$class->throw_user("Cannot add other rights while 'none' right exists");
55
56	    }elsif ( $accessright->access eq 'none' && $ar->access ne 'none' ){
57		$logger->debug("GroupRight::insert: Removing ".$ar->access." access on ".
58			       $ar->object_class." id ".$ar->object_id);
59		$ar->delete();
60	    }
61	}
62    }
63    return $class->SUPER::insert($argv);
64}
65
66=head1 AUTHOR
67
68Carlos Vicente, C<< <cvicente at ns.uoregon.edu> >>
69
70=head1 COPYRIGHT & LICENSE
71
72Copyright 2012 University of Oregon, all rights reserved.
73
74This program is free software; you can redistribute it and/or modify
75it under the terms of the GNU General Public License as published by
76the Free Software Foundation; either version 2 of the License, or
77(at your option) any later version.
78
79This program is distributed in the hope that it will be useful, but
80WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
81or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
82License for more details.
83
84You should have received a copy of the GNU General Public License
85along with this program; if not, write to the Free Software Foundation,
86Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
87
88=cut
89
901;
91