summaryrefslogtreecommitdiff
path: root/LedgerSMB/Admin.pm
blob: d2c303fd203ee43fd57ad94cb6b497eaf70e74a8 (plain)
  1. package LedgerSMB::DBObject::Admin;
  2. use base LedgerSMB::DBObject;
  3. use LedgerSMB::DBObject::Location;
  4. use LedgerSMB::DBObject::Employee;
  5. use LedgerSMB::DBObject::Contact;
  6. sub save_user {
  7. my $self = shift @_;
  8. my $entity_id = shift @{ $self->exec_method( funcname => "save_user" ) };
  9. $self->merge($entity_id);
  10. my $employee = LedgerSMB::DBObject::Employee->new(base=>$self, copy=>'list',
  11. merge=>[
  12. 'salutation',
  13. 'first_name',
  14. 'last_name',
  15. 'employeenumber',
  16. ]
  17. );
  18. $employee->{entity_id} = $entity_id->{id};
  19. $employee->save_employee();
  20. my $loc = LedgerSMB::DBObject::Location->new(base=>$self, copy=>'list',
  21. merge=>[
  22. 'address1',
  23. 'address2',
  24. 'city',
  25. 'state',
  26. 'zipcode',
  27. 'country',
  28. 'companyname',
  29. ]
  30. );
  31. $loc->save_location();
  32. $loc->join_to_person(person=>$employee);
  33. my $contact = LedgerSMB::DBObject::Contact->new(base=>$self, copy=>'list',
  34. merge=>[
  35. 'workphone',
  36. 'homephone',
  37. 'email',
  38. ]
  39. );
  40. $contact->save_homephone(person=>$employee);
  41. $contact->save_workphone(person=>$employee);
  42. $contact->save_email(person=>$employee);
  43. my $roles = $self->exec_method( funcname => "all_roles" );
  44. my $user_roles = $self->exec_method(funcname => "get_user_roles", args=>[ $self->{ modifying_user } ] );
  45. my %active_roles;
  46. for my $role (@{$user_roles}) {
  47. # These are our user's roles.
  48. $active_roles{$role} = 1;
  49. }
  50. my $status;
  51. for my $role ( @{ $roles } ) {
  52. # These roles are were ALL checked on the page, so they're the active ones.
  53. if ($active_roles{$role} && $self->{incoming_roles}->{$role}) {
  54. # do nothing.
  55. }
  56. elsif ($active_roles{$role} && !($self->{incoming_roles}->{$role} )) {
  57. # do remove function
  58. $status = $self->exec_method(funcname => "remove_user_from_role",
  59. args=>[ $self->{ modifying_user }, $role ]
  60. }
  61. elsif ($self->{incoming_roles}->{$role} and !($active_roles{$role} )) {
  62. # do add function
  63. $status = $self->exec_method(funcname => "add_user_to_role",
  64. args=>[ $self->{ modifying_user }, $role ]
  65. );
  66. }
  67. }
  68. }
  69. sub save_group {
  70. my $self = shift @_;
  71. my $existant = shift @{ $self->exec_method (funcname=> "is_group", args=>[$self->{modifying_group}]) };
  72. my $group = shift @{ $self->exec_method (funcname=> "save_group") };
  73. # first we grab all roles
  74. my $roles = $self->exec_method( funcname => "all_roles" );
  75. my $user_roles = $self->exec_method(funcname => "get_user_roles",
  76. args=>[ $self->{ group_name } ]
  77. );
  78. my %active_roles;
  79. for my $role (@{$user_roles}) {
  80. # These are our user's roles.
  81. $active_roles{$role} = 1;
  82. }
  83. my $status;
  84. for my $role ( @{ $roles } ) {
  85. # These roles are were ALL checked on the page, so they're the active ones.
  86. if ($active_roles{$role} && $self->{incoming_roles}->{$role}) {
  87. # we don't need to do anything.
  88. }
  89. elsif ($active_roles{$role} && !($self->{incoming_roles}->{$role} )) {
  90. # do remove function
  91. $status = $self->exec_method(
  92. funcname => "remove_group_from_role",
  93. args=>[ $self->{ modifying_user }, $role ]
  94. );
  95. }
  96. elsif ($self->{incoming_roles}->{$role} and !($active_roles{$role} )) {
  97. # do add function
  98. $status = $self->exec_method(
  99. funcname => "add_group_to_role",
  100. args=>[ $self->{ modifying_user }, $role ]
  101. );
  102. }
  103. }
  104. }
  105. sub delete_user {
  106. my $self = shift @_;
  107. my $status = shift @{ $self->exec_method(funcname=>'delete_user', args=>[$self->{modifying_user}]) };
  108. if ($status) {
  109. return 1;
  110. } else {
  111. my $error = LedgerSMB::Error->new("Delete user failed.");
  112. $error->set_status($status);
  113. return $error;
  114. }
  115. }
  116. sub delete_group {
  117. my $self = shift @_;
  118. my $status = shift @{ $self->exec_method(funcname=>'delete_group', args=>[$self->{groupname}])};
  119. if ($status) {
  120. return 1;
  121. } else {
  122. my $error = LedgerSMB::Error->new("Delete group failed.");
  123. $error->set_status($status);
  124. return $error;
  125. }
  126. }
  127. sub get_entire_user {
  128. my $self = shift @_;
  129. my $id = shift @_;
  130. my $user = {};
  131. my $u = LedgerSMB::DBObject::User->new(base=>$self,copy=>'all');
  132. $user->{user} = $u->get($id);
  133. $user->{pref} = $u->preferences($id);
  134. $user->{employee} = $u->employee($user->{user}->{entity_id});
  135. $user->{person} = $u->person($user->{user}->{entity_id});
  136. $user->{entity} = $u->entity($id);
  137. $user->{roles} = $u->roles($id);
  138. return $user;
  139. }
  140. 1;