summaryrefslogtreecommitdiff
path: root/scripts/admin.pl
blob: 7ba5a40e8bb60b44cfe473b42ba1638b543b34ee (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. salutations=>$admin->get_salutations(),
  67. }
  68. );
  69. }
  70. else {
  71. # print STDERR Dumper($user);
  72. # print STDERR Dumper(@all_roles);
  73. # print STDERR Dumper($user->{roles});
  74. $template->render(
  75. {
  76. user=>$user,
  77. roles=>@all_roles,
  78. user_roles=>$user->{roles},
  79. salutations=>$admin->get_salutations(),
  80. }
  81. );
  82. }
  83. }
  84. sub edit_group {
  85. my ($request) = @_;
  86. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  87. my $all_roles = $admin->role_list();
  88. my $template = LedgerSMB::Template->new(
  89. user => $user,
  90. template => 'Admin/edit_group',
  91. language => $user->{language},
  92. format => 'HTML',
  93. path=>'UI'
  94. );
  95. if ($request->type() eq "POST") {
  96. my $role = $admin->save_role();
  97. return $template->render(
  98. {
  99. user=> $request->{role},
  100. roles=>$all_roles,
  101. user_roles=>$admin->get_user_roles($request->{role})
  102. }
  103. );
  104. }
  105. else {
  106. return $template->render(
  107. {
  108. roles=>$all_roles
  109. }
  110. );
  111. }
  112. }
  113. sub create_group {
  114. my ($request) = @_;
  115. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  116. my $all_roles = $admin->get_roles();
  117. my $template = LedgerSMB::Template->new(
  118. user => $user,
  119. template => 'Admin/edit_group',
  120. language => $user->{language},
  121. format => 'HTML',
  122. path=>'UI'
  123. );
  124. if ($request->type() eq "POST") {
  125. my $role = $admin->save_role();
  126. return $template->render(
  127. {
  128. user=> $role, roles=>$all_roles
  129. }
  130. );
  131. }
  132. else {
  133. return $template->render({roles=>$all_roles});
  134. }
  135. }
  136. sub delete_group {
  137. my ($request) = @_;
  138. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  139. # requires the field modifying_user to be set.
  140. my $status = $admin->delete_group($request->{modifying_user});
  141. # status can either be 1, or an error.
  142. # if there's an error, $status->throw() is called by admin.pm. Or possibly
  143. # in the template itself.
  144. my $template = LedgerSMB::Template->new ( user=>$user,
  145. template=>'Admin/delete_group', language=>$user->{language},
  146. format=>'HTML', path=>'UI');
  147. $template->render($status);
  148. }
  149. sub delete_user {
  150. my ($request) = @_;
  151. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  152. # requires the field modifying_user to be set.
  153. my $status = $admin->delete_user($request->{modifying_user});
  154. # status can either be 1, or an error.
  155. # if there's an error, $status->throw() is called by admin.pm. Or possibly
  156. # in the template itself.
  157. my $template = LedgerSMB::Template->new ( user=>$user,
  158. template=>'Admin/delete_user', language=>$user->{language},
  159. format=>'HTML', path=>'UI');
  160. $template->render($status);
  161. }
  162. sub new_group {
  163. my ($request) = @_;
  164. my $template = LedgerSMB::Template->new( user=>$user,
  165. template=>'Admin/new_group', language=>$user->{language},
  166. format=>'HTML', path=>'UI');
  167. $template->render();
  168. }
  169. sub cancel {
  170. &main(@_);
  171. }
  172. sub __default {
  173. &main(@_);
  174. }
  175. sub main {
  176. my ($request) = @_;
  177. my $template;
  178. my $user = LedgerSMB::DBObject::User->new(base=>$request, copy=>'all');
  179. my $ret = $user->get_all_users();
  180. $template = LedgerSMB::Template->new(
  181. user=>$user,
  182. template=>'Admin/main',
  183. language=>$user->{language},
  184. format=>'HTML',
  185. path=>'UI'
  186. );
  187. $template->render( { users=>$user->{users} } );
  188. }
  189. #eval { do "scripts/custom/admin.pl"};
  190. 1;