summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject/Admin.pm
blob: 52e1d8b3bc6321c3d9698660b57e3cccab2412d4 (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_user {
  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 {
  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=>[ $user->{id} ] );
  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->{$role} ) {
  85. # do nothing.
  86. ;
  87. }
  88. elsif ($active_roles{$role} && !($self->{$role} )) {
  89. # do remove function
  90. $status = $self->exec_method(procname => "admin__remove_user_from_role",
  91. args=>[ $self->{ modifying_user }, $role ] );
  92. }
  93. elsif ($self->{incoming_roles}->{$role} and !($active_roles{$role} )) {
  94. # do add function
  95. $status = $self->exec_method(procname => "admin__add_user_to_role",
  96. args=>[ $self->{ modifying_user }, $role ]
  97. );
  98. }
  99. }
  100. }
  101. sub save_group {
  102. my $self = shift @_;
  103. my $existant = shift @{ $self->exec_method (procname=> "is_group", args=>[$self->{modifying_group}]) };
  104. my $group = shift @{ $self->exec_method (procname=> "save_group") };
  105. # first we grab all roles
  106. my $roles = $self->exec_method( procname => "admin__all_roles" );
  107. my $user_roles = $self->exec_method(procname => "admin__get_user_roles",
  108. args=>[ $self->{ group_name } ]
  109. );
  110. my %active_roles;
  111. for my $role (@{$user_roles}) {
  112. # These are our user's roles.
  113. $active_roles{$role} = 1;
  114. }
  115. my $status;
  116. for my $role ( @{ $roles } ) {
  117. # These roles are were ALL checked on the page, so they're the active ones.
  118. if ($active_roles{$role} && $self->{incoming_roles}->{$role}) {
  119. # we don't need to do anything.
  120. }
  121. elsif ($active_roles{$role} && !($self->{incoming_roles}->{$role} )) {
  122. # do remove function
  123. $status = $self->exec_method(
  124. procname => "admin__remove_group_from_role",
  125. args=>[ $self->{ modifying_user }, $role ]
  126. );
  127. }
  128. elsif ($self->{incoming_roles}->{$role} and !($active_roles{$role} )) {
  129. # do add function
  130. $status = $self->exec_method(
  131. procname => "admin__add_group_to_role",
  132. args=>[ $self->{ modifying_user }, $role ]
  133. );
  134. }
  135. }
  136. }
  137. sub delete_user {
  138. my $self = shift @_;
  139. my $status = shift @{ $self->exec_method(procname=>'admin__delete_user',
  140. args=>[$self->{modifying_user}])
  141. };
  142. if ($status) {
  143. return 1;
  144. } else {
  145. $self->error('Delete user failed.');
  146. #my $error = LedgerSMB::Error->new("Delete user failed.");
  147. #$error->set_status($status);
  148. #return $error;
  149. }
  150. }
  151. sub delete_group {
  152. my $self = shift @_;
  153. my $status = shift @{ $self->exec_method(procname=>'admin__delete_group',
  154. args=>[$self->{groupname}]) }
  155. ;
  156. if ($status) {
  157. return 1;
  158. } else {
  159. $self->error('Delete group failed.');
  160. #my $error = LedgerSMB::Error->new("Delete group failed.");
  161. #$error->set_status($status);
  162. #return $error;
  163. }
  164. }
  165. sub get_salutations {
  166. my $self = shift;
  167. my $sth = $self->{dbh}->prepare("SELECT * FROM salutation ORDER BY id ASC");
  168. $sth->execute();
  169. # Returns a list of hashrefs
  170. return $sth->fetchall_arrayref( {} );
  171. }
  172. sub get_roles {
  173. # These are direct, assignable roles.
  174. my $self = shift;
  175. return $self->exec_method( procname => "admin__all_roles" );
  176. }
  177. 1;