[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
SF.net SVN: ledger-smb:[4909] trunk
- Subject: SF.net SVN: ledger-smb:[4909] trunk
- From: ..hidden..
- Date: Mon, 18 Jun 2012 10:08:45 +0000
Revision: 4909
http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=4909&view=rev
Author: einhverfr
Date: 2012-06-18 10:08:45 +0000 (Mon, 18 Jun 2012)
Log Message:
-----------
Moved user management into employee screen/workflow so existing employees can be turned into users
Modified Paths:
--------------
trunk/LedgerSMB/Scripts/employee.pm
trunk/sql/modules/admin.sql
Added Paths:
-----------
trunk/LedgerSMB/DBObject/Entity/User.pm
Added: trunk/LedgerSMB/DBObject/Entity/User.pm
===================================================================
--- trunk/LedgerSMB/DBObject/Entity/User.pm (rev 0)
+++ trunk/LedgerSMB/DBObject/Entity/User.pm 2012-06-18 10:08:45 UTC (rev 4909)
@@ -0,0 +1,160 @@
+=head1 NAME
+
+LedgerSMB::DBObject::Entity::User - User management Logic for LedgerSMB
+
+=cut
+
+package LedgerSMB::DBObject::Entity::User;
+use Moose;
+extends 'LedgerSMB::DBObject_Moose';
+
+=head1 SYNOPSYS
+
+Resetting a password (expires in 24 hrs):
+ my $user = LedgerSMB::DBObject::Entity::User->get($entity_id);
+ my $user->reset_password('temporary_password');
+
+Creating a new user:
+ my $user = LedgerSMB::DBObject::Entity::User->new(%$request);
+ $user->save;
+
+Saving permissions:
+ my $user = LedgerSMB::DBObject::Entity::User->new(%$request);
+ $user->set_roles($request);
+
+=head1 PROPERTIES
+
+=over
+
+=item entity_id
+
+This is the integer id of the entity of the user
+
+=cut
+
+has entity_id => (is => 'ro', isa => 'Int');
+
+=item username
+
+Username of the individual. Would be the name of a valid Pg role.
+
+=cut
+
+has username => (is => 'rw', isa => 'Str');
+
+=item pls_import
+
+If this flag is set, we don't try to set a password on creating a new user. Also
+we don't create the user account. This assumes that we are making a
+pre-existing PostgreSQL user into a LedgerSMB user.
+
+=cut
+
+has pls_import => (is => 'rw', isa => 'Bool');
+
+=item password
+
+This is only used for new users. It sets a temporary password (good for 24 hrs)
+
+=cut
+
+has password => (is => 'rw', isa => 'Str');
+
+=item role_list
+
+A list of role names granted to the user.
+
+=cut
+
+has role_list => (is => 'rw', isa => 'Maybe[ArrayRef[Str]]');
+
+
+=back
+
+=head1 METHODS
+
+=over
+
+=item get($entity_id)
+
+Returns the user object for that entity id.
+
+=cut
+
+sub get {
+ my ($self, $entity_id) = @_;
+ my ($ref) = __PACKAGE__->call_procedure(
+ procname => 'admin__get_user', args => [$entity_id]
+ );
+ $self->prepare_dbhash($ref);
+ my @roles = __PACKAGE__->call_procedure(
+ procname => 'admin__get_roles_for_user', args => [$entity_id]
+ );
+ $_ = $_->{admin__get_roles_for_user} for (@roles);
+ $ref->{role_list} = ..hidden..;
+ return $self->new(%$ref);
+}
+
+=item reset_password($password)
+
+Resets a user's password to a temporary password good for 24 hours.
+
+=cut
+
+sub reset_password{
+ my ($self, $password) = @_;
+ $self->password($password);
+ my ($ref) = $self->exec_method({funcname => 'admin__save_user'});
+ $self->password(undef);
+}
+
+=item create
+
+Creates the new user.
+
+=cut
+
+sub create{
+ my ($self) = @_;
+ my ($ref) = $self->exec_method({funcname => 'admin__save_user'});
+ $self->password(undef);
+}
+
+=item save_roles($role_list)
+
+Saves (grants) roles requested.
+
+=cut
+
+sub save_roles{
+ my ($self, $role_list) = @_;
+ for my $rol_name (@$role_list) {
+ $self->call_procedure(procname => 'admin__add_user_to_role',
+ args => [$self->{username}, $rol_name]);
+ }
+}
+
+=item list_roles
+
+Lists roles for database.
+
+=cut
+
+sub list_roles{
+ my ($self) = @_;
+ return __PACKAGE__->call_procedure(procname => 'admin__get_roles');
+}
+
+=back
+
+=head1 COPYRIGHT
+
+Copyright (C) 2012 The LedgerSMB Core Team. This file may be reused under the
+conditions of the GNU GPL v2 or at your option any later version. Please see
+the accompanying LICENSE.TXT for more information.
+
+=cut
+
+__PACKAGE__->meta->make_immutable;
+
+1;
Modified: trunk/LedgerSMB/Scripts/employee.pm
===================================================================
--- trunk/LedgerSMB/Scripts/employee.pm 2012-06-18 08:17:51 UTC (rev 4908)
+++ trunk/LedgerSMB/Scripts/employee.pm 2012-06-18 10:08:45 UTC (rev 4909)
@@ -21,6 +21,7 @@
use LedgerSMB::DBObject::Entity::Contact;
use LedgerSMB::DBObject::Entity::Bank;
use LedgerSMB::DBObject::Entity::Note;
+use LedgerSMB::DBObject::Entity::User;
use LedgerSMB::App_State;
use LedgerSMB::Template;
@@ -79,8 +80,10 @@
sub _main_screen {
my ($request, $employee) = @_;
-
-
+ my $user;
+ if ($employee->{entity_id}){
+ $user = LedgerSMB::DBObject::Entity::User->get($employee->{entity_id});
+ }
# DIVS logic
my @DIVS;
if ($employee->{entity_id}){
@@ -182,12 +185,15 @@
my @entity_classes = $request->call_procedure(
procname => 'entity__list_classes'
);
+ my @roles = LedgerSMB::DBObject::Entity::User->list_roles;
$template->render({
DIVS => ..hidden..,
DIV_LABEL => \%DIV_LABEL,
request => $request,
employee => $employee,
+ user => $user,
+ roles => ..hidden..,
country_list => ..hidden..,
locations => ..hidden..,
contacts => ..hidden..,
@@ -401,6 +407,58 @@
get($request);
}
+=item create_user
+
+This turns the employee into a user.
+
+=cut
+
+sub create_user {
+ my ($request) = @_;
+ if ($request->close_form){
+ my $user = LedgerSMB::DBObject::Entity::User->new(%$request);
+ $user->create;
+ }
+ get($request);
+}
+
+=item reset_password
+
+This resets the user's password
+
+=cut
+
+sub reset_password {
+ my ($request) = @_;
+ if ($request->close_form){
+ my $user = LedgerSMB::DBObject::Entity::User->new(%$request);
+ $user->reset_password($request->{password});
+ }
+ get($request);
+}
+
+=item save_roles
+
+Saves the user's permissions
+
+=cut
+
+sub save_roles {
+ my ($request) = @_;
+ if ($request->close_form){
+ my $user = LedgerSMB::DBObject::Entity::User->get($request->{entity_id});
+ my $roles;
+ for my $key(keys %$request){
+ if ($key =~ $request->{_role_prefix} and $request->{key}){
+ push @$roles, $request->{key};
+ }
+ }
+ $user->role_list($roles);
+ $user->save_roles;
+ }
+ get($request);
+}
+
=back
=head1 COPYRIGHT
Modified: trunk/sql/modules/admin.sql
===================================================================
--- trunk/sql/modules/admin.sql 2012-06-18 08:17:51 UTC (rev 4908)
+++ trunk/sql/modules/admin.sql 2012-06-18 10:08:45 UTC (rev 4909)
@@ -219,13 +219,13 @@
--$$ language 'plpgsql';
-CREATE OR REPLACE FUNCTION admin__get_user(in_user_id INT) returns setof users as $$
+CREATE OR REPLACE FUNCTION admin__get_user(in_entity_id INT) returns setof users as $$
DECLARE
a_user users;
BEGIN
- select * into a_user from users where id = in_user_id;
+ select * into a_user from users where entity_id = in_entity_id;
return next a_user;
return;
@@ -535,28 +535,6 @@
remove a login-capable user.
$$;
-CREATE OR REPLACE FUNCTION admin__list_roles(in_username text)
-RETURNS SETOF text AS
-$$
-DECLARE out_rolename RECORD;
-BEGIN
- FOR out_rolename IN
- SELECT rolname FROM pg_roles
- WHERE oid IN (SELECT id FROM connectby (
- '(SELECT m.member, m.roleid, r.oid FROM pg_roles r
- LEFT JOIN pg_auth_members m ON (r.oid = m.roleid)) a',
- 'oid', 'member', 'oid', '320461', '0', ','
- ) c(id integer, parent integer, "level" integer,
- path text, list_order integer)
- )
- LOOP
- RETURN NEXT out_rolename.rolname;
- END LOOP;
-END;
-$$ LANGUAGE PLPGSQL SECURITY DEFINER;
-
-REVOKE execute on function admin__list_roles(in_username text) from public;
-
-- TODO: Add admin user
--CREATE OR REPLACE FUNCTION admin_audit_log () returns int as $$
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.