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