summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject/Admin.pm
blob: 6b54a7d4119a62c19962e929bf15e2690f112234 (plain)
  1. package LedgerSMB::DBObject::Admin;
  2. use base LedgerSMB::DBObject;
  3. use LedgerSMB::Location;
  4. use LedgerSMB::DBObject::Employee;
  5. use LedgerSMB::Contact;
  6. use LedgerSMB::Entity;
  7. #[18:00:31] <aurynn> I'd like to split them employee/user and roles/prefs
  8. #[18:00:44] <aurynn> edit/create employee and add user features if needed.
  9. sub save {
  10. $self->error("Cannot save an Adminstrator object.");
  11. }
  12. sub save_employee {
  13. my $self = shift @_;
  14. my $entity = LedgerSMB::DBObject::Entity->new(base=>$self, copy=>'none');
  15. $entity->set(name=>$self->{first_name}." ".$self->{last_name});
  16. $entity->save();
  17. $self->{entity_id} = $entity->{id};
  18. my $user_id = shift @{ $self->exec_method( procname => "admin_save_user" ) };
  19. $self->merge($user_id);
  20. my $person = LedgerSMB::DBObject::Person->new( base=>$self, copy=>'list',
  21. merge=>[
  22. 'salutation',
  23. 'first_name',
  24. 'last_name',
  25. ]
  26. );
  27. my $employee = LedgerSMB::DBObject::Employee->new( base=>$self, copy=>'list',
  28. merge=>[
  29. 'salutation',
  30. 'first_name',
  31. 'last_name',
  32. 'employeenumber',
  33. ]
  34. );
  35. $employee->{entity_id} = $entity_id->{id};
  36. $employee->save();
  37. my $loc = LedgerSMB::DBObject::Location->new(base=>$self, copy=>'list',
  38. merge=>[
  39. 'address1',
  40. 'address2',
  41. 'city',
  42. 'state',
  43. 'zipcode',
  44. 'country',
  45. 'companyname',
  46. ]
  47. );
  48. $loc->save();
  49. $employee->set_location($loc->{id});
  50. $loc->(person=>$employee);
  51. my $workphone = LedgerSMB::Contact->new(base=>$self);
  52. my $homephone = LedgerSMB::Contact->new(base=>$self);
  53. my $email = LedgerSMB::Contact->new(base=>$self);
  54. $workphone->set(person=>$employee, class=>1, contact=>$self->{workphone});
  55. $homephone->set(person=>$employee, class=>11, contact=>$self->{homephone});
  56. $email->set(person=>$employee, class=>12, contact=>$self->{email});
  57. $workphone->save();
  58. $homephone->save();
  59. $email->save();
  60. # now, check for user-specific stuff. Is this person a user or not?
  61. my $user = LedgerSMB::DBObject::User->new(base=>$self, copy=>'list',
  62. merge=>[
  63. 'username',
  64. 'password',
  65. 'is_a_user'
  66. ]
  67. );
  68. $user->get();
  69. $user->save();
  70. }
  71. sub save_roles_preferences {
  72. my $self = shift @_;
  73. my $user = LedgerSMB::DBObject::User->new( base=>$self, copy=>'all' );
  74. my $roles = $self->exec_method( procname => "admin_all_roles" );
  75. my $user_roles = $self->exec_method(procname => "admin_get_user_roles", args=>[ $self->{ username } ] );
  76. my %active_roles;
  77. for my $role (@{$user_roles}) {
  78. # These are our user's roles.
  79. $active_roles{$role} = 1;
  80. }
  81. my $status;
  82. for my $role ( @{ $roles } ) {
  83. # These roles are were ALL checked on the page, so they're the active ones.
  84. if ($active_roles{$role} && $self->{incoming_roles}->{$role}) {
  85. # do nothing.
  86. }
  87. elsif ($active_roles{$role} && !($self->{incoming_roles}->{$role} )) {
  88. # do remove function
  89. $status = $self->exec_method(procname => "remove_user_from_role",
  90. args=>[ $self->{ modifying_user }, $role ] );
  91. }
  92. elsif ($self->{incoming_roles}->{$role} and !($active_roles{$role} )) {
  93. # do add function
  94. $status = $self->exec_method(procname => "add_user_to_role",
  95. args=>[ $self->{ modifying_user }, $role ]
  96. );
  97. }
  98. }
  99. }
  100. sub save_group {
  101. my $self = shift @_;
  102. my $existant = shift @{ $self->exec_method (procname=> "is_group", args=>[$self->{modifying_group}]) };
  103. my $group = shift @{ $self->exec_method (procname=> "save_group") };
  104. # first we grab all roles
  105. my $roles = $self->exec_method( procname => "all_roles" );
  106. my $user_roles = $self->exec_method(procname => "get_user_roles",
  107. args=>[ $self->{ group_name } ]
  108. );
  109. my %active_roles;
  110. for my $role (@{$user_roles}) {
  111. # These are our user's roles.
  112. $active_roles{$role} = 1;
  113. }
  114. my $status;
  115. for my $role ( @{ $roles } ) {
  116. # These roles are were ALL checked on the page, so they're the active ones.
  117. if ($active_roles{$role} && $self->{incoming_roles}->{$role}) {
  118. # we don't need to do anything.
  119. }
  120. elsif ($active_roles{$role} && !($self->{incoming_roles}->{$role} )) {
  121. # do remove function
  122. $status = $self->exec_method(
  123. procname => "remove_group_from_role",
  124. args=>[ $self->{ modifying_user }, $role ]
  125. );
  126. }
  127. elsif ($self->{incoming_roles}->{$role} and !($active_roles{$role} )) {
  128. # do add function
  129. $status = $self->exec_method(
  130. procname => "add_group_to_role",
  131. args=>[ $self->{ modifying_user }, $role ]
  132. );
  133. }
  134. }
  135. }
  136. sub delete_user {
  137. my $self = shift @_;
  138. my $status = shift @{ $self->exec_method(procname=>'delete_user',
  139. args=>[$self->{modifying_user}])
  140. };
  141. if ($status) {
  142. return 1;
  143. } else {
  144. $self->error('Delete user failed.');
  145. #my $error = LedgerSMB::Error->new("Delete user failed.");
  146. #$error->set_status($status);
  147. #return $error;
  148. }
  149. }
  150. sub delete_group {
  151. my $self = shift @_;
  152. my $status = shift @{ $self->exec_method(procname=>'delete_group',
  153. args=>[$self->{groupname}]) }
  154. ;
  155. if ($status) {
  156. return 1;
  157. } else {
  158. $self->error('Delete group failed.');
  159. #my $error = LedgerSMB::Error->new("Delete group failed.");
  160. #$error->set_status($status);
  161. #return $error;
  162. }
  163. }
  164. sub get_salutations {
  165. my $self = shift;
  166. my $sth = $self->{dbh}->prepare("SELECT * FROM salutation ORDER BY id ASC");
  167. $sth->execute();
  168. # Returns a list of hashrefs
  169. return $sth->fetchall_arrayref( {} );
  170. }
  171. 1;