[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb:[3447] trunk



Revision: 3447
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=3447&view=rev
Author:   einhverfr
Date:     2011-07-05 14:17:22 +0000 (Tue, 05 Jul 2011)

Log Message:
-----------
user import is mostly working.  Needs some additional debugging

Modified Paths:
--------------
    trunk/LedgerSMB/DBObject/Admin.pm
    trunk/LedgerSMB/DBObject/User.pm
    trunk/LedgerSMB.pm
    trunk/UI/Admin/edit_user.html
    trunk/scripts/admin.pl
    trunk/sql/modules/admin.sql

Modified: trunk/LedgerSMB/DBObject/Admin.pm
===================================================================
--- trunk/LedgerSMB/DBObject/Admin.pm	2011-07-05 13:17:59 UTC (rev 3446)
+++ trunk/LedgerSMB/DBObject/Admin.pm	2011-07-05 14:17:22 UTC (rev 3447)
@@ -40,14 +40,16 @@
         ]
     );
     $user->{entity_id} = $employee->{entity_id};
-    $user->save();
+    if ($user->save() == 8){ # Duplicate User exception
+        return 8;
+    }
     $self->{user} = $user;
     $self->{employee} = $employee;
 
-    if ($self->{password}){
+    if ($self->{password} or $self->{import}){
        return $self->{dbh}->commit;
     }
- 
+
     my $loc = LedgerSMB::DBObject::Location->new(base=>$self, copy=>'list', 
         merge=>[
             'address1',
@@ -60,10 +62,10 @@
         ]
     );
      
+    $loc->{type} = 'person';
     $loc->save();
     $employee->set_location($loc->{id});
     $loc->(person=>$employee);
-        
     my $workphone = LedgerSMB::Contact->new(base=>$self);
     my $homephone = LedgerSMB::Contact->new(base=>$self);
     my $email = LedgerSMB::Contact->new(base=>$self);

Modified: trunk/LedgerSMB/DBObject/User.pm
===================================================================
--- trunk/LedgerSMB/DBObject/User.pm	2011-07-05 13:17:59 UTC (rev 3446)
+++ trunk/LedgerSMB/DBObject/User.pm	2011-07-05 14:17:22 UTC (rev 3447)
@@ -119,14 +119,30 @@
     $self->{password_expires} = $pw_expiration->{user__check_my_expiration};
 }
 
+
+# Return codes:  0 as success, 8 as duplicate user, and 1 as general failure
 sub save {
     
     my $self = shift @_;
     my $user = $self->get();
     
     
-    # doesn't check for the password - that's done in the sproc.
-    my ($ref) = $self->exec_method(funcname=>'admin__save_user'); 
+    # doesn't check for the password - that's done in the sproc. --Aurynn
+    # Note here that we pass continue_on_error to the sproc and handle
+    # any exceptions ourselves --CT
+    my ($ref) = $self->exec_method(funcname=>'admin__save_user',
+                          continue_on_error=> 1);
+
+    # Handling exceptions here
+    if (!$ref) { # Unsuccessful
+        if ($@ =~ /No password/){
+              $self->error($self->{_locale}->text('Password required'));
+        } elsif ($@ eq 'Duplicate User'){
+              $self->{dbh}->rollback;
+              return 8;
+        }
+
+    } 
     ($self->{id}) = values %$ref;
     if (!$self->{id}) {
         

Modified: trunk/LedgerSMB.pm
===================================================================
--- trunk/LedgerSMB.pm	2011-07-05 13:17:59 UTC (rev 3446)
+++ trunk/LedgerSMB.pm	2011-07-05 14:17:22 UTC (rev 3447)
@@ -754,8 +754,13 @@
     my $sth = $self->{dbh}->prepare($query);
     if (scalar @call_args){
         $query_rc = $sth->execute(@call_args);
-        if (!$query_rc and !$args{continue_on_error}){
-              $self->dberror($self->{dbh}->errstr . ": " . $query);
+        if (!$query_rc){
+              if ($args{continue_on_error} and  #  only for plpgsql exceptions
+                              ($self->{dbh}->errstr =~ /^P/)){
+                    $@ = $self->{dbh}->errstr;
+              } else {
+                    $self->dberror($self->{dbh}->errstr . ": " . $query);
+              }
         }
     } else {
         $query_rc = $sth->execute();

Modified: trunk/UI/Admin/edit_user.html
===================================================================
--- trunk/UI/Admin/edit_user.html	2011-07-05 13:17:59 UTC (rev 3446)
+++ trunk/UI/Admin/edit_user.html	2011-07-05 14:17:22 UTC (rev 3447)
@@ -27,8 +27,31 @@
                     <input type="password" name="password" value="<?lsmb user.user.password ?>"/>
                 </td>
             </tr>
-            
             <tr>
+                <td><?lsmb text('Import') ?></td>
+                <?lsmb 
+                  IF import;
+                      importc1 = 'CHECKED';
+                      importc0 = '';
+                  ELSE;
+                      importc1 = '';
+                      importc0 = 'CHECKED';
+                  END; ?> 
+                <td><?lsmb INCLUDE input element_data = {
+                           label   = 'Yes'
+                           value   = '1'
+                           checked = importc1
+                           name    = 'import'
+                           type    = 'radio'
+                   };
+                   INCLUDE input element_data = {
+                           label   = 'No'
+                           value   = '0'
+                           checked = importc0
+                           name    = 'import'
+                           type    = 'radio'
+                   } ?>
+            <tr>
                 <td>
                     <select name="salutation">
                         <?lsmb FOREACH sal = salutations ?>

Modified: trunk/scripts/admin.pl
===================================================================
--- trunk/scripts/admin.pl	2011-07-05 13:17:59 UTC (rev 3446)
+++ trunk/scripts/admin.pl	2011-07-05 14:17:22 UTC (rev 3447)
@@ -65,11 +65,19 @@
 
 sub save_user {
     my ($request, $admin) = @_;
+    if ($request->{import}){
+         delete $request->{password};
+    }
     my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
     
     my $sal = $admin->get_salutations();
     
     my $entity = $admin->save_user();
+    if ($entity == 8){ # Duplicate user
+          $request->{import} = 1;
+          __edit_page($request);
+          return;
+    }
     my $groups = $admin->get_roles();
     $admin->{stylesheet} = $request->{stylesheet};
     __edit_page($admin);

Modified: trunk/sql/modules/admin.sql
===================================================================
--- trunk/sql/modules/admin.sql	2011-07-05 13:17:59 UTC (rev 3446)
+++ trunk/sql/modules/admin.sql	2011-07-05 14:17:22 UTC (rev 3447)
@@ -324,33 +324,34 @@
         p_id int;
         l_id int;
         stmt text;
+        t_is_role bool;
+        t_is_user bool;
     BEGIN
         -- WARNING TO PROGRAMMERS:  This function runs as the definer and runs
         -- utility statements via EXECUTE.
         -- PLEASE BE VERY CAREFUL ABOUT SQL-INJECTION INSIDE THIS FUNCTION.
 
-        IF in_import IS NOT TRUE THEN
-             PERFORM rolname FROM pg_roles WHERE rolname = in_username;
-             IF FOUND THEN
-                 RAISE EXCEPTION 'Duplicate user';
-             END IF;
+       PERFORM rolname FROM pg_roles WHERE rolname = in_username;
+       t_is_role := found;
+       t_is_user := admin__is_user(in_username);
+
+       IF t_is_role and t_is_user is false and in_import is false THEN
+          RAISE EXCEPTION 'Duplicate user';
         END IF;
-    
-        if admin__is_user(in_username) then
-                
+
+        if t_is_role and in_password is not null then
                 execute 'ALTER USER ' || quote_ident( in_username ) || 
                      ' WITH ENCRYPTED PASSWORD ' || quote_literal (in_password)
-                     || $e$ valid until $e$ || quote_literal(now() + '1 day'::interval);
-        else
-            if in_password IS NULL THEN
+                     || $e$ valid until $e$ || 
+                      quote_literal(now() + '1 day'::interval);
+        elsif in_password IS NULL and t_is_role is false THEN
                 RAISE EXCEPTION 'No password';
-            end if;
-           
+        elsif  t_is_role is false THEN
             -- create an actual user
                 execute 'CREATE USER ' || quote_ident( in_username ) || 
                      ' WITH ENCRYPTED PASSWORD ' || quote_literal (in_password)
                      || $e$ valid until $e$ || quote_literal(now() + '1 day'::interval);
-            end if;         
+       END IF;         
         
         select * into a_user from users lu where lu.id = in_id;
         IF FOUND THEN 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.