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