From d340ccd5ecbb731c273a07876ad0dfc0a9b33379 Mon Sep 17 00:00:00 2001 From: aurynn_cmd Date: Thu, 15 Nov 2007 23:10:31 +0000 Subject: Significant work on the Entity and Administrative code. It's buggy, so please test, etc. thoroughly. git-svn-id: https://ledger-smb.svn.sourceforge.net/svnroot/ledger-smb/trunk@1869 4979c152-3d1c-0410-bac9-87ea11338e46 --- LedgerSMB/DBObject/Admin.pm | 24 ++++++++++----- LedgerSMB/DBObject/Customer.pm | 54 ++++++++++++++++++++------------- LedgerSMB/DBObject/User.pm | 38 +++++++++++++++++++---- LedgerSMB/Employee.pm | 5 +--- LedgerSMB/Entity.pm | 43 ++++++++++++++++++++++++++ LedgerSMB/Vendor.pm | 46 +++++++++++++++------------- UI/Admin/main.html | 2 +- UI/get_password.html | 30 ++++++++++++------- UI/login.js | 14 ++++++++- lsmb-request.pl | 22 +++++++++----- scripts/admin.pl | 10 +++++-- scripts/customer.pl | 68 +++++++++++++++++++++++++++++++----------- scripts/login.pl | 45 ++++++++++++++++++++++++---- sql/modules/admin.sql | 47 ++++++++++++++++++++++++++++- 14 files changed, 345 insertions(+), 103 deletions(-) create mode 100644 LedgerSMB/Entity.pm diff --git a/LedgerSMB/DBObject/Admin.pm b/LedgerSMB/DBObject/Admin.pm index 51620c47..6b54a7d4 100644 --- a/LedgerSMB/DBObject/Admin.pm +++ b/LedgerSMB/DBObject/Admin.pm @@ -5,6 +5,7 @@ use base LedgerSMB::DBObject; use LedgerSMB::Location; use LedgerSMB::DBObject::Employee; use LedgerSMB::Contact; +use LedgerSMB::Entity; #[18:00:31] I'd like to split them employee/user and roles/prefs #[18:00:44] edit/create employee and add user features if needed. @@ -18,19 +19,27 @@ sub save_employee { my $self = shift @_; - my $entity_id = shift @{ $self->exec_method( procname => "save_user" ) }; - $self->merge($entity_id); + my $entity = LedgerSMB::DBObject::Entity->new(base=>$self, copy=>'none'); - my $person = LedgerSMB::DBObject::Person->new(base=>$self, copy=>'list', + $entity->set(name=>$self->{first_name}." ".$self->{last_name}); + $entity->save(); + + $self->{entity_id} = $entity->{id}; + + + my $user_id = shift @{ $self->exec_method( procname => "admin_save_user" ) }; + $self->merge($user_id); + + my $person = LedgerSMB::DBObject::Person->new( base=>$self, copy=>'list', merge=>[ 'salutation', 'first_name', 'last_name', ] ); - my $employee = LedgerSMB::DBObject::Employee->new(base=>$self, copy=>'list', + my $employee = LedgerSMB::DBObject::Employee->new( base=>$self, copy=>'list', merge=>[ - '', + 'salutation', 'first_name', 'last_name', 'employeenumber', @@ -76,16 +85,15 @@ sub save_employee { 'is_a_user' ] ); - $user->get(); $user->save(); } -sub save_roles { +sub save_roles_preferences { my $self = shift @_; - my $user = LedgerSMB::DBObject::User->new(base=>$self, copy=>'all'); + my $user = LedgerSMB::DBObject::User->new( base=>$self, copy=>'all' ); my $roles = $self->exec_method( procname => "admin_all_roles" ); my $user_roles = $self->exec_method(procname => "admin_get_user_roles", args=>[ $self->{ username } ] ); diff --git a/LedgerSMB/DBObject/Customer.pm b/LedgerSMB/DBObject/Customer.pm index ef25c4a1..36b4ed62 100644 --- a/LedgerSMB/DBObject/Customer.pm +++ b/LedgerSMB/DBObject/Customer.pm @@ -1,33 +1,47 @@ package LedgerSMB::DBObject::Customer; -use base qw(LedgerSMB); +use base qw(LedgerSMB::DBObject::Company); use LedgerSMB::DBObject; +use LedgerSMB::Entity; -sub save_to_db { +sub save { + + # this is doing way too much. my $self = shift @_; - my $id; - if ($self->{id} >= 1) { - $id = $self->{id}; + my $entity; + + # this is a fairly effective way of telling if we need to create a new + # entity or not. + + if (!$self->{entity_id}) { + + $entity = LedgerSMB::Entity->new(base=>$request); } else { - $id = $self->next_customer_id(); + + $entity = LedgerSMB::Entity->get(id=>$self->{entity_id}); } - $id = $self->save($id, $self->{discount}, $self->{tax_included}, - $self->{creditlimit}, $self->{terms}, $self->{customernumber}, - $self->{cc}, $self->{bcc}, $self->{business_id}, $self->{language}, - $self->{pricegroup}, $self->{currency}, $self->{startdate}, - $self->{enddate} - ); - - # Undef in the created field causes the system to use now() as the current - # creation date. - $self->location_save( - $id, 1, $self->{line_one}, $self->{line_two}, $self->{line_three}, - $self->{city_province}, $self->{mailing_code}, $self->{country}, undef + + $entity->set(name=> $reqeust->{first_name}." ".$request->{last_name} ); + $entity->set(entity_class=>2); + + $self->set(entity_id=>$entity->{id}); + $self->set(entity_class=> 2); + + $entity->save(); + if (!self->{entity_id}) { - ); - return $id; + $self->{entity_id} = $entity->{id}; + } + $self->SUPER::save(); + + return $self->{id}; +} + +sub search { + + } 1; \ No newline at end of file diff --git a/LedgerSMB/DBObject/User.pm b/LedgerSMB/DBObject/User.pm index 62ae4e0b..3486fbd6 100644 --- a/LedgerSMB/DBObject/User.pm +++ b/LedgerSMB/DBObject/User.pm @@ -8,13 +8,13 @@ sub save { my $user = $self->get(); - if ($user->{id} && $self->{is_a_user}) { + if ( $user->{id} && $self->{is_a_user} ) { # doesn't check for the password - that's done in the sproc. - $self->{id} = @{ $self->exec_method(procname=>'admin_save_user', - args=>[$user->{id}, $self->{username}, $self->{password}] ) }[0]; - else { - + $self->{id} = shift @{ $self->exec_method(procname=>'admin_save_user', + args=>[$user->{id}, $self->{username}, $self->{password}] ) }; + if (!$self->{id}) { + return 0; } } @@ -26,6 +26,12 @@ sub save { return $self->remove(); } + elsif ($self->{is_a_user}) { + + # No user ID, meaning, creating a new one. + $self->{id} = shift @{ $self->exec_method(procname=>'admin_save_user', + args=>[undef, $self->{username}, $self->{password}] ) }; + } return 1; } @@ -49,4 +55,26 @@ sub remove { return $code->[0]; } +sub save_prefs { + + my $self = shift @_; + + my $pref_id = $self->exec_method(procname=>"admin_save_preferences", + args=>[ + 'language', + 'stylesheet', + 'printer', + 'dateformat', + 'numberformat' + ] + ); +} + +sub get_all_users { + + my $self = shift @_; + + $self->{users} = $self->exec_method( procname=>"user_get_all_users" ); +} + 1; \ No newline at end of file diff --git a/LedgerSMB/Employee.pm b/LedgerSMB/Employee.pm index 59a550f2..e792aa84 100644 --- a/LedgerSMB/Employee.pm +++ b/LedgerSMB/Employee.pm @@ -48,10 +48,7 @@ our $VERSION = '1.0.0'; sub save { my $self = shift; - - my $person = shift @{ $self->exec_method (procname => 'person_save', - args => [] )}; - + my $hashref = shift @{ $self->exec_method( procname => "employee_save" ) }; $self->merge( $hashref, 'id' ); } diff --git a/LedgerSMB/Entity.pm b/LedgerSMB/Entity.pm new file mode 100644 index 00000000..4c01b804 --- /dev/null +++ b/LedgerSMB/Entity.pm @@ -0,0 +1,43 @@ +package LedgerSMB::Entity; + +use base qw/LedgerSMB::DBObject/; + +sub save { + + my $self = shift @_; + + my $id = @{ $self->exec_method(procname=>'entity_save', + args=>[ + $self->{name}, + $self->{entity_class}, + ] + )}; + + $self->{id} = shift @{ $id }; + return $self->{id}; +} + +sub get { + + my $self = shift @_; + + my $hashref = $self->exec_method(procname=>'entity_get', args=>[$self->{id}]); + $self->merge($hashref); + return $self->{id}; + +} + +sub search { + + # Shouldn't really be necessary.. + + # anyway, Search on name. + my $self = shift @_; + + my @list = @{ $self->exec_method( procname=>'entity_search', + args=>[ + $self->{name} + ] + ) }; +} +1; \ No newline at end of file diff --git a/LedgerSMB/Vendor.pm b/LedgerSMB/Vendor.pm index 52aa4944..fe58b79f 100644 --- a/LedgerSMB/Vendor.pm +++ b/LedgerSMB/Vendor.pm @@ -1,33 +1,39 @@ package LedgerSMB::DBObject::Vendor; -use base qw(LedgerSMB); +use base qw(LedgerSMB::DBObject::Company); use LedgerSMB::DBObject; -sub save_to_db { - +sub save { my $self = shift @_; - my $id; - if ($self->{id} >= 1) { - $id = $self->{id}; + my $entity; + + # this is a fairly effective way of telling if we need to create a new + # entity or not. + + if (!$self->{entity_id}) { + + $entity = LedgerSMB::Entity->new(base=>$request); } else { - $id = $self->next_vendor_id(); + + $entity = LedgerSMB::Entity->get(id=>$self->{entity_id}); } - $id = $self->save($id, $self->{discount}, $self->{tax_included}, - $self->{creditlimit}, $self->{terms}, $self->{vendornumber}, - $self->{cc}, $self->{bcc}, $self->{business_id}, $self->{language}, - $self->{pricegroup}, $self->{currency}, $self->{startdate}, - $self->{enddate} - ); - # Undef in the created field causes the system to use now() as the current - # creation date. - $self->location_save( - $id, 1, $self->{line_one}, $self->{line_two}, $self->{line_three}, - $self->{city_province}, $self->{mailing_code}, $self->{country}, undef + $entity->set(name=> $reqeust->{first_name}." ".$request->{last_name} ); + $entity->set(entity_class=>2); + + $self->set(entity_id=>$entity->{id}); + $self->set(entity_class=> 1); + + $entity->save(); + if (!self->{entity_id}) { - ); - return $id; + $self->{entity_id} = $entity->{id}; + } + $self->SUPER::save(); + + return $self->{id}; + } 1; \ No newline at end of file diff --git a/UI/Admin/main.html b/UI/Admin/main.html index a7e07c73..adb03840 100644 --- a/UI/Admin/main.html +++ b/UI/Admin/main.html @@ -19,7 +19,7 @@ Date Added - + diff --git a/UI/get_password.html b/UI/get_password.html index 774caea3..43e57eff 100644 --- a/UI/get_password.html +++ b/UI/get_password.html @@ -7,17 +7,27 @@ + - + + @@ -25,13 +35,13 @@ function focus_input(){

-
+ -
+ diff --git a/UI/login.js b/UI/login.js index fd421e26..aa2af97c 100644 --- a/UI/login.js +++ b/UI/login.js @@ -11,7 +11,7 @@ function get_http_request_object(){ function submit_form() { var http = get_http_request_object(); - var username = document.login.login.value; + var username = document.login.login.value; var password = document.login.password.value; http.open("get", 'login.pl?action=authenticate&company=' + document.login.company.value, false, @@ -25,3 +25,15 @@ function submit_form() { document.location = document.login.action + "?action=login&company="+ documnet.login.company.value; } + +function check_auth() { + + var http = get_http_request_object(); + var username = "admin"; + var password = document.login.password.value; + + http.open("get", "login.pl?action=authenticate&company=" + + document.login.company.value, false, + username, password + ); +} \ No newline at end of file diff --git a/lsmb-request.pl b/lsmb-request.pl index dcaa5cf4..3a33d278 100644 --- a/lsmb-request.pl +++ b/lsmb-request.pl @@ -47,13 +47,21 @@ if (!$script){ $request->error($locale->text('No workflow script specified')); } -eval { require "scripts/$script" } - || $request->error($locale->text('Unable to open script') . ": scripts/$script : $!"); +&call_script( $script, $request ); -$script =~ s/\.pl$//; -$script = "LedgerSMB::Scripts::$script"; -$script->can($request->{action}) - || $request->error($locale->text("Action Not Defined: ") . $request->{action}); +sub call_script { + + my $script = shift @_; + my $request = shift @_; -$script->can( $request->{action} )->($request); + eval { require "scripts/$script" } + || $request->error($locale->text('Unable to open script') . ": scripts/$script : $!"); + + $script =~ s/\.pl$//; + $script = "LedgerSMB::Scripts::$script"; + $script->can($request->{action}) + || $request->error($locale->text("Action Not Defined: ") . $request->{action}); + + $script->can( $request->{action} )->($request); +} 1; diff --git a/scripts/admin.pl b/scripts/admin.pl index 18fb0ca8..2b8ca816 100644 --- a/scripts/admin.pl +++ b/scripts/admin.pl @@ -5,6 +5,7 @@ require 'lsmb-request.pl'; use LedgerSMB::Template; use LedgerSMB::DBObject::Admin; +use LedgerSMB::DBObject::User; sub new_user { @@ -23,7 +24,7 @@ sub new_user { my $template = LedgerSMB::Template->new( user => $user, - template => 'Admin/edit_user', language => $user->{language}, + template => 'Admin/edit_user', language => $user->{ language }, format => 'HTML', path=>'UI'); $template->render( @@ -153,11 +154,14 @@ sub __default { my ($class, $request) = @_; - # TODO: check for login stuff. my $template; + my $user = LedgerSMB::DBObject::User->new(base=>$request, copy=>'all'); + + $user->get_all_users(); + $template = LedgerSMB::Template->new( user=>$user, template=>'Admin/main', language=>$user->{language}, format=>'HTML', path=>'UI'); - $template->render(); + $template->render($user); } 1; \ No newline at end of file diff --git a/scripts/customer.pl b/scripts/customer.pl index 24fb04f6..7fc238da 100644 --- a/scripts/customer.pl +++ b/scripts/customer.pl @@ -44,11 +44,13 @@ of the customer informations. sub get { my ($request) = @_; - my $customer = LedgerSMB::DBObject::Customer->new(base => $request, copy => 'all'); - my $result = $customer->get($customer->{id}); + my $customer = LedgerSMB::DBObject::Company->new(base => $request, copy => 'all'); + + $customer->set( entity_class=> '2' ); + my $result = $customer->get(); my $template = LedgerSMB::Template->new( user => $user, - template => 'customer.html', language => $user->{language}, + template => 'Customer/customer.html', language => $user->{language}, format => 'html'); $template->render($results); @@ -68,10 +70,10 @@ This method creates a blank screen for entering a customer's information. sub add { my ($request) = @_; - my $customer = LedgerSMB::DBObject::Customer->new(base => $request, copy => 'all'); - + my $customer = LedgerSMB::DBObject::Company->new(base => $request, copy => 'all'); + $customer->set(entity_class=>2); my $template = LedgerSMB::Template->new( user => $user, - template => 'customer.html', language => $user->{language}, + template => 'Customer/customer.html', language => $user->{language}, format => 'html'); $template->render($results); } @@ -94,13 +96,30 @@ as well as customer/Company name. sub search { my ($request) = @_; - my $customer = LedgerSMB::DBObject::Customer->new(base => $request, copy => 'all'); - my $results = $customer->search($customer->{search_pattern}); - my $template = LedgerSMB::Template->new( user => $user, - template => 'customer_search.html', language => $user->{language}, - format => 'html'); - $template->render($results); + if ($request->type() eq 'POST') { + # assume it's asking us to do the search, now + + my $customer = LedgerSMB::DBObject::Company->new(base => $request, copy => 'all'); + $customer->set(entity_class=>2); + my $results = $customer->search($customer->{search_pattern}); + + my $template = LedgerSMB::Template->new( user => $user, + template => 'Customer/customer.html', language => $user->{language}, + format => 'html'); + $template->render($results); + + } + else { + + # grab the happy search page out. + + my $template = LedgerSMB::Template->new( user => $user, + template => 'Customer/customer_search.html', language => $user->{language}, + format => 'html'); + + $template->render(); + } } =pod @@ -119,13 +138,26 @@ customer as needed, and will generate a new Company ID for the customer if neede sub save { my ($request) = @_; - my $customer = LedgerSMB::DBObject::Customer->new(base => $request, copy => 'all'); - my $result = $customer->save_to_db(); - my $template = LedgerSMB::Template->new( user => $user, - template => 'customer.html', language => $user->{language}, - format => 'html'); - $template->render($result); + if ($request->type() == 'POST') { + + my $customer = LedgerSMB::DBObject::Customer->get(base=>$request, copy=>'all'); + + unless ($customer) { + + $customer = LedgerSMB::DBObject::Customer->new(base=>$reqest, copy=>'all'); + } + + my $result = $customer->save(); + + my $template = LedgerSMB::Template->new( user => $user, + template => 'Customer/customer.html', language => $user->{language}, + format => 'html'); + $template->render($result); + } + else { + + } } 1; diff --git a/scripts/login.pl b/scripts/login.pl index 1ce79587..537d8b61 100644 --- a/scripts/login.pl +++ b/scripts/login.pl @@ -7,13 +7,18 @@ use LedgerSMB::User; use LedgerSMB::Auth; use strict; +# this is kind of silly, as it doesn't check if someone IS trying to log in. +# If one looks at the login template (get_password.html), it does not post +# to any action, so this code will always get called, thereby preventing +# anyone from actually logging in. + sub __default { my ($request) = @_; my $locale; $locale = LedgerSMB::Locale->get_handle(${LedgerSMB::Sysconfig::language}) or $request->error( __FILE__ . ':' . __LINE__ . - ": Locale not loaded: $!\n" ); - my $template = LedgerSMB::Template->new( + ": Locale not loaded: $!\n" ); + my $template = LedgerSMB::Template->new( user =>$request->{_user}, locale => $locale, path => 'UI', @@ -23,6 +28,8 @@ sub __default { $template->render($request); } +# Directly printing like this is made of fail. + sub authenticate { my ($request) = @_; if (!$request->{dbh}){ @@ -33,10 +40,20 @@ sub authenticate { } my $path = $ENV{SCRIPT_NAME}; $path =~ s|[^/]*$||; - if ($request->{dbh} || $request->{log_out}){ + + if ($request->{dbh} && $request->{next}) { + + print "Content-Type: text/html\n"; + print "Set-Cookie: LedgerSMB=Login; path=$path\n"; + print "Status: 302 Found\n"; + print "Location: ".$path.$request->{next}."\n"; + print "\n"; + exit; + } + elsif ($request->{dbh} || $request->{log_out}){ print "Content-Type: text/html\n"; print "Set-Cookie: LedgerSMB=Login; path=$path\n"; - print "Status: 200 Success\n\n"; + print "Status: 200 Success\n\n"; if ($request->{log_out}){ exit; } @@ -44,7 +61,7 @@ sub authenticate { else { print "WWW-Authenticate: Basic realm=\"LedgerSMB\"\n"; print "Status: 401 Unauthorized\n\n"; - print "Please enter your credentials.\n"; + print "Please enter your credentials.\n"; exit; } } @@ -69,5 +86,23 @@ sub logout { print "Content-type: text/html\n\n"; exit; } + +sub continue { + + my ($request) = @_; + + if ($request->{next} && $request->{password}) { + + $request->{user} = "admin"; + + if (&authenticate($request)) { +# LedgerSMB::Handler::call_script(); + } + } + else { + # well, wtf? This is kind of useless. + $request->error("Cannot continue to a Nonexistent page."); + } +} 1; diff --git a/sql/modules/admin.sql b/sql/modules/admin.sql index 84b65af3..dfd702c8 100644 --- a/sql/modules/admin.sql +++ b/sql/modules/admin.sql @@ -264,11 +264,13 @@ CREATE OR REPLACE FUNCTION admin_save_user( -- create an actual user v_user_id := nextval('users_id_seq'); - insert into users (id, name, entity_id) VALUES ( + insert into users (id, username, entity_id) VALUES ( v_user_id, in_username, in_entity_id ); + + insert into user_preference (id) values (v_user_id); -- Finally, issue the create user statement @@ -412,4 +414,47 @@ $$ LANGUAGE PLPGSQL; --$$ language plpgsql; +create or replace function admin_is_user (in_user text) returns bool as $$ + DECLARE + pg_user pg_roles; + + BEGIN + + select * into pg_user from pg_roles where rolname = in_user; + + IF NOT FOUND THEN + return 'f'::bool; + END IF; + return 't'::bool; + + END; + +$$ language plpgsql; + +create or replace function admin_is_user (in_user text) returns bool as $$ + + BEGIN + + return 'f'::bool; + + END; + +$$ language plpgsql; + +create or replace view user_listable as + select + u.id, + u.username, + e.created + from entity e + join users u on u.entity_id = e.id; + + +create or replace function user_get_all_users () returns setof user_listable as $$ + + select * from user_listable; + +$$ language sql; + + commit; \ No newline at end of file -- cgit v1.2.3