summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject/User.pm
blob: 00a7e9dbcf1b3c970506b293941d63e42bb1bf36 (plain)
  1. package LedgerSMB::DBObject::User;
  2. use base qw/LedgerSMB::DBObject/;
  3. use Data::Dumper;
  4. sub save {
  5. my $self = shift @_;
  6. my $user = $self->get();
  7. if ( $user->{id} && $self->{is_a_user} ) {
  8. # doesn't check for the password - that's done in the sproc.
  9. $self->{id} = shift @{ $self->exec_method(funcname=>'admin__save_user',
  10. args=>[$user->{id}, $self->{username}, $self->{password}] ) };
  11. if (!$self->{id}) {
  12. return 0;
  13. }
  14. }
  15. elsif ($user && !$self->{is_a_user}) {
  16. # there WAS a user, and we've decided we want that user to go away now.
  17. $self->{id} = $user->{id};
  18. return $self->remove();
  19. }
  20. elsif ($self->{is_a_user}) {
  21. # No user ID, meaning, creating a new one.
  22. $self->{id} = shift @{ $self->exec_method(funcname=>'admin__save_user',
  23. args=>[undef, $self->{username}, $self->{password}] ) };
  24. }
  25. return 1;
  26. }
  27. sub get {
  28. my $self = shift @_;
  29. my $id = shift;
  30. my @users = $self->exec_method(
  31. funcname=>'admin__get_user',
  32. args=>[$id]
  33. );
  34. $self->{user} = $users[0];
  35. my @prefs = $self->exec_method(
  36. funcname=>'admin__user_preferences',
  37. args=>[$id]
  38. );
  39. $self->{pref} = $prefs[0];
  40. # $self->{person} = @{ $self->exec_method(
  41. # funcname=>'admin__user_preferences',
  42. # args=>[$self->{user}->{entity_id}]
  43. # )
  44. # }[0];
  45. my @emp = $self->exec_method(
  46. funcname=>'employee__get',
  47. args=>[$self->{user}->{entity_id}]
  48. );
  49. $self->{employee} = $emp[0];
  50. my @ent = $self->exec_method(
  51. funcname=>'entity__get_entity',
  52. args=>[ $self->{user}->{entity_id} ]
  53. );
  54. $self->{entity} = $ent[0];
  55. my @roles = $self->exec_method(
  56. funcname=>'admin__get_roles_for_user',
  57. args=>[$id]
  58. );
  59. # Now, location and stuff.
  60. my @loc = $self->exec_method(
  61. funcname=>'person__list_locations',
  62. args=>[ $self->{user}->{entity_id} ]
  63. );
  64. $self->{locations} = \@loc;
  65. my @contacts = $self->exec_method(
  66. funcname=>"person__list_contacts",
  67. args=>[$self->{user}->{entity_id} ]
  68. );
  69. $self->{contacts} = \@contacts;
  70. my @rolstore;
  71. for my $role (@roles) {
  72. my $rolname = $role->{'admin__get_roles_for_user'};
  73. my $company = $self->{company};
  74. $rolname =~ s/lsmb_${company}__//gi;
  75. push @rolstore, $rolname; # Only one key=>value pair
  76. }
  77. $self->{roles} = \@rolstore;
  78. $self->{entity_id} = $self->{entity}->{id};
  79. print STDERR "Got all user information";
  80. #$user->{user} = $u->get($id);
  81. #$user->{pref} = $u->preferences($id);
  82. #$user->{employee} = $u->employee($user->{user}->{entity_id});
  83. #$user->{person} = $u->person($user->{user}->{entity_id});
  84. #$user->{entity} = $u->entity($id);
  85. #$user->{roles} = $u->roles($id);
  86. }
  87. sub remove {
  88. my $self = shift;
  89. my $code = $self->exec_method(funcname=>"admin__delete_user", args=>[$self->{id}, $self->{username}]);
  90. $self->{id} = undef; # never existed..
  91. return $code->[0];
  92. }
  93. sub save_prefs {
  94. my $self = shift @_;
  95. my $pref_id = $self->exec_method(funcname=>"admin__save_preferences",
  96. args=>[
  97. 'language',
  98. 'stylesheet',
  99. 'printer',
  100. 'dateformat',
  101. 'numberformat'
  102. ]
  103. );
  104. }
  105. sub get_all_users {
  106. my $self = shift @_;
  107. my @ret = $self->exec_method( funcname=>"user__get_all_users" );
  108. $self->{users} = \@ret;
  109. }
  110. sub roles {
  111. my $self = shift @_;
  112. my $id = shift @_;
  113. }
  114. sub save_contact {
  115. my $self = shift @_;
  116. my $id = shift @_;
  117. my $class = shift @_;
  118. my $contact = shift @_;
  119. my @ret;
  120. print STDERR Dumper($self->{entity}->{id});
  121. if ($id) {
  122. print STDERR "Found ID..";
  123. @ret = $self->exec_method(funcname=>"person__save_contact",
  124. args=>[
  125. $self->{entity}->{id},
  126. $self->{contacts}->[$id]->{contact_class},
  127. $self->{contacts}->[$id]->{contact},
  128. $contact
  129. ]
  130. );
  131. }
  132. else{
  133. print STDERR "Did not find an ID, attempting to save a new contact..\n";
  134. print STDERR ($class."\n");
  135. print STDERR ($contact."\n");
  136. print STDERR ($self->{entity_id}."\n");
  137. @ret = $self->exec_method(funcname=>"person__save_contact",
  138. args=>[
  139. $self->{entity_id},
  140. $class,
  141. undef,
  142. $contact
  143. ]
  144. );
  145. }
  146. print STDERR Dumper(\@ret);
  147. if ($ret[0]->{person__save_contact} == 1){
  148. $self->{dbh}->commit();
  149. }
  150. else{
  151. $self->error("Couldn't save contact...");
  152. }
  153. return 1;
  154. }
  155. sub delete_contact {
  156. my $self = shift @_;
  157. my $id = shift @_;
  158. # Okay
  159. # ID doesn't actually conform to any database entry
  160. # We're basically cheating outrageously here.
  161. }
  162. 1;