summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject/User.pm
blob: 0ca6bcfc17dfd89c62b26d01947bfc918d323af9 (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. print STDERR "Got all user information";
  79. #$user->{user} = $u->get($id);
  80. #$user->{pref} = $u->preferences($id);
  81. #$user->{employee} = $u->employee($user->{user}->{entity_id});
  82. #$user->{person} = $u->person($user->{user}->{entity_id});
  83. #$user->{entity} = $u->entity($id);
  84. #$user->{roles} = $u->roles($id);
  85. }
  86. sub remove {
  87. my $self = shift;
  88. my $code = $self->exec_method(funcname=>"admin__delete_user", args=>[$self->{id}, $self->{username}]);
  89. $self->{id} = undef; # never existed..
  90. return $code->[0];
  91. }
  92. sub save_prefs {
  93. my $self = shift @_;
  94. my $pref_id = $self->exec_method(funcname=>"admin__save_preferences",
  95. args=>[
  96. 'language',
  97. 'stylesheet',
  98. 'printer',
  99. 'dateformat',
  100. 'numberformat'
  101. ]
  102. );
  103. }
  104. sub get_all_users {
  105. my $self = shift @_;
  106. my @ret = $self->exec_method( funcname=>"user__get_all_users" );
  107. $self->{users} = \@ret;
  108. }
  109. sub roles {
  110. my $self = shift @_;
  111. my $id = shift @_;
  112. }
  113. sub save_contact {
  114. my $self = shift @_;
  115. my $id = shift @_;
  116. my $contact = shift @_;
  117. my @ret = $self->exec_method(funcname=>"person__save_contact",
  118. args=>[
  119. $self->{entity_id},
  120. $self->{contacts}->[$id]->{contact_class},
  121. $self->{contacts}->[$id]->{contact},
  122. $contact
  123. ]
  124. );
  125. }
  126. 1;