Skip to content

Commit

Permalink
user management now works, needs some documentation
Browse files Browse the repository at this point in the history
git-svn-id: https://ledger-smb.svn.sourceforge.net/svnroot/ledger-smb/trunk@2636 4979c152-3d1c-0410-bac9-87ea11338e46
  • Loading branch information
einhverfr committed Jun 9, 2009
1 parent 6ec49ba commit e583c13
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 61 deletions.
13 changes: 7 additions & 6 deletions LedgerSMB.pm
Expand Up @@ -696,7 +696,7 @@ sub error {

print qq|Content-Type: text/html; charset=utf-8\n\n|;
print "<head></head>";

$self->{msg} =~ s/\n/<br \/>\n/;
print
qq|<body><h2 class="error">Error!</h2> <p><b>$self->{msg}</b></body>|;

Expand Down Expand Up @@ -804,23 +804,24 @@ sub _on_connection_error {
}
}

# Deprecated, only here for old code
sub dberror{
my $self = shift @_;
my $state_error = {
'42401' => $self->{_locale}->text('Access Denied'),
'42883' => $self->{_locale}->text('Internal Database Error'),
'42501' => $self->{_locale}->text('Access Denied'),
'42401' => $self->{_locale}->text('Access Denied'),
'22008' => $self->{_locale}->text('Invalid date/time entered'),
'22012' => $self->{_locale}->text('Division by 0 error'),
'22004' => $self->{_locale}->text('Required input not provided'),
'P0001' => $self->{_locale}->text('Error from Function:') . " " .
$self->{dbh}->errstr,
'23502' => $self->{_locale}->text('Required input not provided'),
'P0001' => $self->{_locale}->text('Error from Function:') . "\n" .
$self->{dbh}->errstr,
};
print STDERR "Logging SQL State ".$self->{dbh}->state.", error ".
$self->{dbh}->err . ", string " .$self->{dbh}->errstr . "\n";
if (scalar grep /^$self->{dbh}->state$/, keys %$state_error){
if (defined $state_error->{$self->{dbh}->state}){
$self->error($state_error->{$self->{dbh}->state});
exit;
}
$self->error($self->{dbh}->state . ":" . $self->{dbh}->errstr);
}
Expand Down
46 changes: 26 additions & 20 deletions LedgerSMB/DBObject/Admin.pm
Expand Up @@ -22,6 +22,9 @@ sub save_user {
my $entity = LedgerSMB::DBObject::Employee->new(base=>$self, copy=>'none');

$entity->set(name=>$self->{first_name}." ".$self->{last_name});
if (!defined $self->{first_name} or !defined $self->{last_name}){
return;
}
$entity->save();

$self->{entity_id} = $entity->{entity};
Expand Down Expand Up @@ -94,57 +97,61 @@ sub save_roles {
my $self = shift @_;

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=>[ $user->{id} ] );

$user->get();
$self->{modifying_user} = $user->{user}->{username};
my @roles = $self->exec_method( funcname => "admin__get_roles" );
my @user_roles = $self->exec_method(funcname => "admin__get_roles_for_user");
my %active_roles;
for my $role (@{ $user_roles }) {
for my $role (@user_roles) {

# These are our user's roles.
print STDERR "Have $role->{rolname}\n";

$active_roles{$role} = 1;
$active_roles{"$role->{rolname}"} = 1;
}

my $status;

for my $role ( @{ $roles } ) {

for my $r ( @roles) {
my $role = $r->{rolname};
my $reqrole = $role;
$reqrole =~ s/^lsmb_$self->{company}__//;
# These roles are were ALL checked on the page, so they're the active ones.

if ( $active_roles{$role} && $self->{$role} ) {
if ( $active_roles{$role} && $self->{$reqrole} ) {

# do nothing.
;
}
elsif ($active_roles{$role} && !($self->{$role} )) {
elsif ($active_roles{$role} && !($self->{$reqrole} )) {

# do remove function
$status = $self->exec_method(procname => "admin__remove_user_from_role",
$status = $self->call_procedure(procname => "admin__remove_user_from_role",
args=>[ $self->{ modifying_user }, $role ] );
}
elsif ($self->{incoming_roles}->{$role} and !($active_roles{$role} )) {
elsif ($self->{$reqrole} and !($active_roles{$role} )) {

# do add function
$status = $self->exec_method(procname => "admin__add_user_to_role",
$status = $self->call_procedure(procname => "admin__add_user_to_role",
args=>[ $self->{ modifying_user }, $role ]
);
}
}
$self->{dbh}->commit;
}

sub save_group {

my $self = shift @_;

my $existant = shift @{ $self->exec_method (procname=> "is_group", args=>[$self->{modifying_group}]) };
my $existant = shift @{ $self->call_procedure (procname=> "is_group", args=>[$self->{modifying_group}]) };

my $group = shift @{ $self->exec_method (procname=> "save_group") };
my $group = shift @{ $self->exec_method (funcname=> "save_group") };

# first we grab all roles

my $roles = $self->exec_method( procname => "admin__all_roles" );
my $user_roles = $self->exec_method(procname => "admin__get_user_roles",
my $roles = $self->call_procedure( procname => "admin__all_roles" );
my $user_roles = $self->call_procedure(procname => "admin__get_user_roles",
args=>[ $self->{ group_name } ]
);

Expand Down Expand Up @@ -242,12 +249,11 @@ sub get_salutations {
sub get_roles {

my $self = shift @_;
# print STDERR "attempting to get roles";
my @s_rows = $self->call_procedure(procname=>'admin__get_roles');
my @rows;
my $company = $self->{company};
for my $role (@s_rows) {
my $rolname = $role->{'admin__get_roles'};
my $company = $self->{company};
my $rolname = $role->{'rolname'};
$rolname =~ s/lsmb_${company}__//gi;
push @rows, $rolname;
}
Expand Down
34 changes: 23 additions & 11 deletions UI/Admin/edit_user.html
Expand Up @@ -229,9 +229,13 @@
</td>
</tr>
</form>
</table>
</table>
<form name="groups" method="POST" action="admin.pl">
<input type="hidden" name="action" value="save_groups"/>
<?lsmb PROCESS input element_data = {
type="hidden"
name="user_id"
value= user.user.id
} ?>
<table>
<!-- Groups section -->
<tr>
Expand All @@ -241,22 +245,30 @@
<tr>
<?lsmb END?>
<td>
<input type="checkbox" name="<?lsmb loop.index?>" value="1"
<?lsmb FOREACH rolname IN user.roles ?>
<?lsmb IF role == rolname?>
checked
<?lsmb END?>
<?lsmb END?> />
<?lsmb role?>
<?lsmb rolcheck = undef;
IF user.roles.grep(role).size;
rolcheck = "checked";
END ?>
<?lsmb PROCESS input element_data = {
type = "checkbox"
label = role
value = 1
name = role
checked = rolcheck
}, label_pos = 1 ?>
</td>
<?lsmb END?>
</tr>
</table>

<table>
<tr>
<td><button value="groups">Save Groups</button></td>
<td><button name="method" value="cancel">Cancel</td>
<td><?lsmb PROCESS button element_data = {
text = text('Save Groups') #'
class = "submit"
name = "action"
value = "save_user"
} ?></td>
</tr>
</table>
</form>
Expand Down
6 changes: 6 additions & 0 deletions UI/lib/elements.html
Expand Up @@ -40,6 +40,7 @@

<?lsmb # INPUT ELEMENT ?>
<?lsmb BLOCK input ?>
<?lsmb IF label_pos != 1 and label_pos != -1; label_pos = -1; END ?>
<?lsmb IF element_data # Only process element if one exists. ?>
<?lsmb
input_defaults = {} # Some inputs have no defaults, so make sure everything is empty to start with.
Expand Down Expand Up @@ -74,8 +75,13 @@
<?lsmb PROCESS custom_attributes # Process custom attributes.
custom_attribute_data=element_data.attributes
?>
<?lsmb IF label_pos == -1 ?>
<?lsmb PROCESS auto_label # Process element label. ?>
<?lsmb END ?>
<input<?lsmb all_attributes ?><?lsmb all_custom_attributes ?> />
<?lsmb IF label_pos == 1 ?>
<?lsmb PROCESS auto_label # Process element label. ?>
<?lsmb END ?>
<?lsmb END ?>
<?lsmb END ?>

Expand Down
25 changes: 4 additions & 21 deletions scripts/admin.pl
Expand Up @@ -41,9 +41,6 @@ sub __edit_page {
contact_classes=>$admin->get_contact_classes(),
locations=>$location->get_all($user_obj->{entity_id},"person"),
};
open (FOO,">/tmp/dump.txt");
print STDERR Dumper($template_data->{contact_classes});
print FOO Dumper($template_data);

for my $key (keys(%{$otd})) {

Expand All @@ -67,6 +64,7 @@ sub save_user {

my $groups = $admin->get_roles();
my $entity = $admin->save_user();
$admin->save_roles();
__edit_page($admin);
}

Expand Down Expand Up @@ -128,23 +126,10 @@ sub edit_user {
contact_classes=>$admin->get_contact_classes(),
locations=>$location->get_all($user_obj->{entity_id},"person"),
};
open (FOO,">/tmp/dump.txt");
print STDERR Dumper($template_data->{contact_classes});
print FOO Dumper($template_data);
if ($request->type() eq 'POST') {

$admin->save_user();
$admin->save_roles();
$template->render($template_data);
}
else {
# print STDERR Dumper($user);
# print STDERR Dumper(@all_roles);
if ($request->{location_id}) {
$template_data->{location} = $location->get($request->{location_id});
}
$template->render($template_data);
if ($request->{location_id}) {
$template_data->{location} = $location->get($request->{location_id});
}
$template->render($template_data);
}

sub edit_group {
Expand Down Expand Up @@ -361,9 +346,7 @@ sub save_location {
# zipcode
# country
# u_id isn't an entity_it, though.
print STDERR "Attempting to save location...\n";
$location->{user_id} = $user_obj->{user}->{entity_id};
print STDERR $location->{user_id}."\n";
my $id = $location->save("person");
# Done and done.

Expand Down
1 change: 1 addition & 0 deletions sql/Pg-database.sql
Expand Up @@ -289,6 +289,7 @@ CREATE TABLE person_to_contact (
person_id integer not null references person(id) ON DELETE CASCADE,
contact_class_id integer references contact_class(id) not null,
contact text check(contact ~ '[[:alnum:]_]') not null,
description text,
PRIMARY KEY (person_id,contact_class_id,contact));

COMMENT ON TABLE person_to_contact IS $$ To keep track of the relationship between multiple contact methods and a single individual $$;
Expand Down
2 changes: 1 addition & 1 deletion sql/modules/Person.sql
Expand Up @@ -112,7 +112,7 @@ $$
DECLARE out_row RECORD;
BEGIN
FOR out_row IN
SELECT cc.class, cc.id, c.contact
SELECT cc.class, cc.id, c.description, c.contact
FROM person_to_contact c
JOIN contact_class cc ON (c.contact_class_id = cc.id)
JOIN person p ON (c.person_id = p.id)
Expand Down
5 changes: 3 additions & 2 deletions sql/modules/admin.sql
Expand Up @@ -10,7 +10,7 @@

create table lsmb_roles (

user_id integer not null references users,
user_id integer not null references users(id),
role text not null

);
Expand Down Expand Up @@ -39,7 +39,8 @@ CREATE OR REPLACE FUNCTION admin__add_user_to_role(in_username TEXT, in_role TEX
stmt := 'GRANT '|| quote_ident(in_role) ||' to '|| quote_ident(in_username);

EXECUTE stmt;
insert into lsmb_roles (user_id, role) values (in_username, in_role);
insert into lsmb_roles (user_id, role)
SELECT id, in_role from users where username = in_username;
return 1;
END;

Expand Down

0 comments on commit e583c13

Please sign in to comment.