summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject/Admin.pm
blob: e09e121449059b6febb904a3cbbcccd0b3635879 (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. sub save_user {
  7. my $self = shift @_;
  8. my $entity_id = shift @{ $self->exec_method( procname => "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 $workphone = LedgerSMB::Contact->new(base=>$self);
  34. my $homephone = LedgerSMB::Contact->new(base=>$self);
  35. my $email = LedgerSMB::Contact->new(base=>$self);
  36. $workphone->set(person=>$employee, class=>1, contact=>$self->{workphone});
  37. $homephone->set(person=>$employee, class=>11, contact=>$self->{homephone});
  38. $email->set(person=>$employee, class=>12, contact=>$self->{email});
  39. $workphone->save();
  40. $homephone->save();
  41. $email->save();
  42. my $roles = $self->exec_method( procname => "all_roles" );
  43. my $user_roles = $self->exec_method(procname => "get_user_roles", args=>[ $self->{ modifying_user } ] );
  44. my %active_roles;
  45. for my $role (@{$user_roles}) {
  46. # These are our user's roles.
  47. $active_roles{$role} = 1;
  48. }
  49. my $status;
  50. for my $role ( @{ $roles } ) {
  51. # These roles are were ALL checked on the page, so they're the active ones.
  52. if ($active_roles{$role} && $self->{incoming_roles}->{$role}) {
  53. # do nothing.
  54. }
  55. elsif ($active_roles{$role} && !($self->{incoming_roles}->{$role} )) {
  56. # do remove function
  57. $status = $self->exec_method(procname => "remove_user_from_role",
  58. args=>[ $self->{ modifying_user }, $role ] );
  59. }
  60. elsif ($self->{incoming_roles}->{$role} and !($active_roles{$role} )) {
  61. # do add function
  62. $status = $self->exec_method(procname => "add_user_to_role",
  63. args=>[ $self->{ modifying_user }, $role ]
  64. );
  65. }
  66. }
  67. }
  68. sub save_group {
  69. my $self = shift @_;
  70. my $existant = shift @{ $self->exec_method (procname=> "is_group", args=>[$self->{modifying_group}]) };
  71. my $group = shift @{ $self->exec_method (procname=> "save_group") };
  72. # first we grab all roles
  73. my $roles = $self->exec_method( procname => "all_roles" );
  74. my $user_roles = $self->exec_method(procname => "get_user_roles",
  75. args=>[ $self->{ group_name } ]
  76. );
  77. my %active_roles;
  78. for my $role (@{$user_roles}) {
  79. # These are our user's roles.
  80. $active_roles{$role} = 1;
  81. }
  82. my $status;
  83. for my $role ( @{ $roles } ) {
  84. # These roles are were ALL checked on the page, so they're the active ones.
  85. if ($active_roles{$role} && $self->{incoming_roles}->{$role}) {
  86. # we don't need to do anything.
  87. }
  88. elsif ($active_roles{$role} && !($self->{incoming_roles}->{$role} )) {
  89. # do remove function
  90. $status = $self->exec_method(
  91. procname => "remove_group_from_role",
  92. args=>[ $self->{ modifying_user }, $role ]
  93. );
  94. }
  95. elsif ($self->{incoming_roles}->{$role} and !($active_roles{$role} )) {
  96. # do add function
  97. $status = $self->exec_method(
  98. procname => "add_group_to_role",
  99. args=>[ $self->{ modifying_user }, $role ]
  100. );
  101. }
  102. }
  103. }
  104. sub delete_user {
  105. my $self = shift @_;
  106. my $status = shift @{ $self->exec_method(procname=>'delete_user',
  107. args=>[$self->{modifying_user}])
  108. };
  109. if ($status) {
  110. return 1;
  111. } else {
  112. $self->error('Delete user failed.');
  113. #my $error = LedgerSMB::Error->new("Delete user failed.");
  114. #$error->set_status($status);
  115. #return $error;
  116. }
  117. }
  118. sub delete_group {
  119. my $self = shift @_;
  120. my $status = shift @{ $self->exec_method(procname=>'delete_group',
  121. args=>[$self->{groupname}]) }
  122. ;
  123. if ($status) {
  124. return 1;
  125. } else {
  126. $self->error('Delete group failed.');
  127. #my $error = LedgerSMB::Error->new("Delete group failed.");
  128. #$error->set_status($status);
  129. #return $error;
  130. }
  131. }
  132. sub get_salutations {
  133. my $self = shift;
  134. my $sth = $self->{dbh}->prepare("SELECT * FROM salutation ORDER BY id ASC");
  135. $sth->execute();
  136. # Returns a list of hashrefs
  137. return $sth->fetchall_arrayref( {} );
  138. }
  139. 1;