summaryrefslogtreecommitdiff
path: root/scripts/admin.pl
blob: a7f8b4a8b2d7ccb883c5bdc3cd1bc62fd2424918 (plain)
  1. #!/usr/bin/perl
  2. package LedgerSMB::Scripts::admin;
  3. require 'lsmb-request.pl';
  4. use LedgerSMB::Template;
  5. use LedgerSMB::DBObject::Admin;
  6. use LedgerSMB::DBObject::User;
  7. sub new_user {
  8. # uses the same page as create_user, only pre-populated.
  9. #my ($class, $request) = @_;
  10. my $request = shift @_;
  11. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  12. my $sal = $admin->get_salutations();
  13. my $groups = $admin->get_roles();
  14. if ($request->type() eq 'POST') {
  15. # do the save stuff
  16. my $entity = $admin->save_new_user();
  17. my $template = LedgerSMB::Template->new( user => $user,
  18. template => 'Admin/edit_user', language => $user->{ language },
  19. format => 'HTML', path=>'UI');
  20. $template->render(
  21. {
  22. user=>$entity,
  23. salutations=>$sal,
  24. roles=>$groups
  25. }
  26. );
  27. } else {
  28. my $template = LedgerSMB::Template->new( user => $user,
  29. template => 'Admin/edit_user', language => $user->{language},
  30. format => 'HTML', path=>'UI');
  31. $template->render(
  32. {
  33. salutations=>$sal,
  34. roles=>$groups
  35. }
  36. );
  37. }
  38. }
  39. sub edit_user {
  40. # uses the same page as create_user, only pre-populated.
  41. my ($class, $request) = @_;
  42. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'user_id');
  43. my $all_roles = $admin->get_roles();
  44. my $template = LedgerSMB::Template->new(
  45. user => $user,
  46. template => 'Admin/edit_user',
  47. language => $user->{language},
  48. format => 'HTML',
  49. path=>'UI'
  50. );
  51. if ($request->type() eq 'POST') {
  52. $admin->save_user();
  53. $admin->save_roles();
  54. $template->render(
  55. {
  56. user=>$admin->get_entire_user(),
  57. roles=>$all_roles,
  58. user_roles=>$admin->get_user_roles($request->{username})
  59. }
  60. );
  61. }
  62. else {
  63. my $edited_user = $admin->get_entire_user();
  64. $template->render(
  65. {
  66. user=>$edited_user,
  67. roles=>$all_roles,
  68. user_roles=>$admin->get_user_roles($request->{username})
  69. }
  70. );
  71. }
  72. }
  73. sub edit_group {
  74. my ($class, $request) = @_;
  75. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  76. my $all_roles = $admin->role_list();
  77. my $template = LedgerSMB::Template->new(
  78. user => $user,
  79. template => 'Admin/edit_group',
  80. language => $user->{language},
  81. format => 'HTML',
  82. path=>'UI'
  83. );
  84. if ($request->type() eq "POST") {
  85. my $role = $admin->save_role();
  86. return $template->render(
  87. {
  88. user=> $request->{role},
  89. roles=>$all_roles,
  90. user_roles=>$admin->get_user_roles($request->{role});
  91. }
  92. );
  93. }
  94. else {
  95. return $template->render(
  96. {
  97. roles=>$all_roles
  98. }
  99. );
  100. }
  101. }
  102. sub create_group {
  103. my ($class, $request) = @_;
  104. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  105. my $all_roles = $admin->get_roles();
  106. my $template = LedgerSMB::Template->new(
  107. user => $user,
  108. template => 'Admin/edit_group',
  109. language => $user->{language},
  110. format => 'HTML',
  111. path=>'UI'
  112. );
  113. if ($request->type() eq "POST") {
  114. my $role = $admin->save_role();
  115. return $template->render(
  116. {
  117. user=> $role, roles=>$all_roles
  118. }
  119. );
  120. }
  121. else {
  122. return $template->render({roles=>$all_roles});
  123. }
  124. }
  125. sub delete_group {
  126. my ($class, $request) = @_;
  127. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  128. # requires the field modifying_user to be set.
  129. my $status = $admin->delete_group($request->{modifying_user});
  130. # status can either be 1, or an error.
  131. # if there's an error, $status->throw() is called by admin.pm. Or possibly
  132. # in the template itself.
  133. my $template = LedgerSMB::Template->new ( user=>$user,
  134. template=>'Admin/delete_group', language=>$user->{language},
  135. format=>'HTML', path=>'UI');
  136. $template->render($status);
  137. }
  138. sub delete_user {
  139. my ($class, $request) = @_;
  140. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  141. # requires the field modifying_user to be set.
  142. my $status = $admin->delete_user($request->{modifying_user});
  143. # status can either be 1, or an error.
  144. # if there's an error, $status->throw() is called by admin.pm. Or possibly
  145. # in the template itself.
  146. my $template = LedgerSMB::Template->new ( user=>$user,
  147. template=>'Admin/delete_user', language=>$user->{language},
  148. format=>'HTML', path=>'UI');
  149. $template->render($status);
  150. }
  151. sub new_group {
  152. my ($class, $request) = @_;
  153. my $template = LedgerSMB::Template->new( user=>$user,
  154. template=>'Admin/new_group', language=>$user->{language},
  155. format=>'HTML', path=>'UI');
  156. $template->render();
  157. }
  158. sub cancel {
  159. __default(@_);
  160. }
  161. sub __default {
  162. my ($class, $request) = @_;
  163. my $template;
  164. my $user = LedgerSMB::DBObject::User->new(base=>$request, copy=>'all');
  165. $user->get_all_users();
  166. $template = LedgerSMB::Template->new( user=>$user,
  167. template=>'Admin/main', language=>$user->{language},
  168. format=>'HTML', path=>'UI');
  169. $template->render($user);
  170. }
  171. eval { do "scripts/custom/admin.pl"};
  172. 1;