summaryrefslogtreecommitdiff
path: root/scripts/admin.pl
blob: 9eeb9454be600327454b9e7a6daca4dfd241e046 (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(
  29. user => $user,
  30. template => 'Admin/edit_user',
  31. language => $user->{language},
  32. format => 'HTML', path=>'UI');
  33. $template->render(
  34. {
  35. salutations=>$sal,
  36. roles=>$groups
  37. }
  38. );
  39. }
  40. }
  41. sub edit_user {
  42. # uses the same page as create_user, only pre-populated.
  43. my ($class, $request) = @_;
  44. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'user_id');
  45. my $all_roles = $admin->get_roles();
  46. my $template = LedgerSMB::Template->new(
  47. user => $user,
  48. template => 'Admin/edit_user',
  49. language => $user->{language},
  50. format => 'HTML',
  51. path=>'UI'
  52. );
  53. if ($request->type() eq 'POST') {
  54. $admin->save_user();
  55. $admin->save_roles();
  56. $template->render(
  57. {
  58. user=>$admin->get_entire_user(),
  59. roles=>$all_roles,
  60. user_roles=>$admin->get_user_roles($request->{username})
  61. }
  62. );
  63. }
  64. else {
  65. my $edited_user = $admin->get_entire_user();
  66. $template->render(
  67. {
  68. user=>$edited_user,
  69. roles=>$all_roles,
  70. user_roles=>$admin->get_user_roles($request->{username})
  71. }
  72. );
  73. }
  74. }
  75. sub edit_group {
  76. my ($class, $request) = @_;
  77. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  78. my $all_roles = $admin->role_list();
  79. my $template = LedgerSMB::Template->new(
  80. user => $user,
  81. template => 'Admin/edit_group',
  82. language => $user->{language},
  83. format => 'HTML',
  84. path=>'UI'
  85. );
  86. if ($request->type() eq "POST") {
  87. my $role = $admin->save_role();
  88. return $template->render(
  89. {
  90. user=> $request->{role},
  91. roles=>$all_roles,
  92. user_roles=>$admin->get_user_roles($request->{role})
  93. }
  94. );
  95. }
  96. else {
  97. return $template->render(
  98. {
  99. roles=>$all_roles
  100. }
  101. );
  102. }
  103. }
  104. sub create_group {
  105. my ($class, $request) = @_;
  106. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  107. my $all_roles = $admin->get_roles();
  108. my $template = LedgerSMB::Template->new(
  109. user => $user,
  110. template => 'Admin/edit_group',
  111. language => $user->{language},
  112. format => 'HTML',
  113. path=>'UI'
  114. );
  115. if ($request->type() eq "POST") {
  116. my $role = $admin->save_role();
  117. return $template->render(
  118. {
  119. user=> $role, roles=>$all_roles
  120. }
  121. );
  122. }
  123. else {
  124. return $template->render({roles=>$all_roles});
  125. }
  126. }
  127. sub delete_group {
  128. my ($class, $request) = @_;
  129. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  130. # requires the field modifying_user to be set.
  131. my $status = $admin->delete_group($request->{modifying_user});
  132. # status can either be 1, or an error.
  133. # if there's an error, $status->throw() is called by admin.pm. Or possibly
  134. # in the template itself.
  135. my $template = LedgerSMB::Template->new ( user=>$user,
  136. template=>'Admin/delete_group', language=>$user->{language},
  137. format=>'HTML', path=>'UI');
  138. $template->render($status);
  139. }
  140. sub delete_user {
  141. my ($class, $request) = @_;
  142. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  143. # requires the field modifying_user to be set.
  144. my $status = $admin->delete_user($request->{modifying_user});
  145. # status can either be 1, or an error.
  146. # if there's an error, $status->throw() is called by admin.pm. Or possibly
  147. # in the template itself.
  148. my $template = LedgerSMB::Template->new ( user=>$user,
  149. template=>'Admin/delete_user', language=>$user->{language},
  150. format=>'HTML', path=>'UI');
  151. $template->render($status);
  152. }
  153. sub new_group {
  154. my ($class, $request) = @_;
  155. my $template = LedgerSMB::Template->new( user=>$user,
  156. template=>'Admin/new_group.html', language=>$user->{language},
  157. format=>'HTML', path=>'UI');
  158. $template->render();
  159. }
  160. sub cancel {
  161. &mainpage(@_);
  162. }
  163. sub __default {
  164. &mainpage(@_);
  165. }
  166. sub mainpage {
  167. my ($request) = @_;
  168. my $template;
  169. # We need to test for a login here first.
  170. my $user = LedgerSMB::DBObject::User->new(base=>$request, copy=>'all');
  171. my $ret = $user->get_all_users();
  172. $template = LedgerSMB::Template->new(
  173. user=>$user,
  174. template=>'Admin/main.html',
  175. language=>$user->{language},
  176. format=>'HTML',
  177. path=>'UI'
  178. );
  179. $template->render({users=>$ret});
  180. }
  181. #eval { do "scripts/custom/admin.pl"};
  182. 1;