Browse Source

user permissions

themage
Marco Neves 7 years ago
parent
commit
3b44ca93e8
3 changed files with 171 additions and 5 deletions
  1. 10 2
      database/tables.sql
  2. 109 0
      lib/oSMo/Db/UserPermissions.pm
  3. 52 3
      lib/oSMo/User.pm

+ 10 - 2
database/tables.sql

@ -33,7 +33,15 @@ CREATE TABLE `osmo__user_settings` (
33 33
  `value` varchar(200) CHARACTER SET utf8 DEFAULT '',
34 34
  PRIMARY KEY (`id`),
35 35
  UNIQUE KEY `user_id` (`user__id`,`name`)
36
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1
36
) DEFAULT CHARSET=latin1;
37
38
CREATE TABLE `osmo__user_permissions` (
39
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
40
  `user__id` int(10) unsigned NOT NULL,
41
  `permission` varchar(50) NOT NULL,
42
  PRIMARY KEY (`id`),
43
  UNIQUE KEY `user__id` (`user__id`,`permission`)
44
) DEFAULT CHARSET=latin1;
37 45
38 46
/* used by anything that needs a temporary token */
39 47
CREATE TABLE `osmo__token` (
@ -57,6 +65,6 @@ CREATE TABLE `osmo__session` (
57 65
  `changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
58 66
  `content` text CHARACTER SET utf8,
59 67
  PRIMARY KEY (`sid`)
60
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
68
) DEFAULT CHARSET=latin1;
61 69
62 70

+ 109 - 0
lib/oSMo/Db/UserPermissions.pm

@ -0,0 +1,109 @@
1
package oSMo::Db::UserPermissions;
2
3
use oSMo::Base qw(oSMo::Db);
4
5
use Scalar::Util qw(blessed);
6
7
BEGIN {
8
	__PACKAGE__->table('osmo__user_permissions');
9
10
}
11
12
has permissions 	=> sub { {} };
13
has to_delete			=> sub { {} };
14
has	changed				=> 0;
15
has	userid				=> 0;
16
17
sub retrieve {
18
	my ($class, $userid) = @_;
19
20
	my $res = $class->fetch_hash(<<EoQ, $userid);
21
SELECT permission, 1 FROM __TABLE__ WHERE user__id = ?
22
EoQ
23
24
	my $self = oSMo::Db::UserPermissions->new();
25
	$self->userid($userid);
26
27
	if ($res) {
28
		$self->permissions($res);
29
	}
30
31
	return $self;
32
}
33
34
sub set {
35
	my $self = shift;
36
	my @to_set = @_;
37
38
	my $perms = $self->permissions();
39
	my $changes = 0;
40
41
	for my $p (@to_set) {
42
		if (!$perms->{$p}) {
43
			$changes++;	
44
			$perms->{$p} = 1;
45
		}
46
	}
47
	$self->changed($changes) if $changes;
48
49
	return;
50
}
51
52
sub get {
53
	my $self	= shift;
54
	my $name	= shift;
55
	my $perms = $self->permissions;
56
57
	return $perms->{$name};
58
}
59
60
sub delete {
61
	my $self = shift;
62
	my @to_del = @_;
63
64
	my $perms = $self->permissions();
65
	my $todel	= $self->to_delete();
66
	my $changes = 0;
67
68
	for my $p (@to_del) {
69
		if (exists $perms->{$p}) {
70
			$changes++;	
71
			$todel->{$p} = 1;
72
			delete $perms->{$p};
73
		}
74
	}
75
	
76
	$self->changed($changes) if $changes;
77
78
	return;
79
}
80
81
sub update {
82
	my $self = shift;
83
	return unless $self->changed;
84
85
	my $to_del = $self->to_delete;
86
	if (keys %$to_del) {
87
		my @sets = keys %$to_del;
88
		my $q = join ', ', map { '?' } @sets;
89
		$self->_do(<<EoQ, $self->userid, @sets );
90
DELETE FROM __TABLE__ WHERE user__id = ? AND permission IN ($q)
91
EoQ
92
93
	} # to delete
94
95
	my $perms = $self->permissions;
96
	if (keys %$perms) {
97
		my $uid = $self->userid;
98
		my @data = map { [$uid => $_ ] } keys %$perms;
99
		(blessed $self)->inserts(<<EoQ, \@data );
100
INSERT IGNORE INTO __TABLE__
101
	(user__id, permission)
102
	VALUES
103
EoQ
104
	}
105
106
	# delete deleted and insert/update all others
107
}
108
109
1;

+ 52 - 3
lib/oSMo/User.pm

@ -3,13 +3,14 @@ package oSMo::User;
3 3
use oSMo::Base '-base';
4 4
5 5
use oSMo::Db::UserSettings;
6
use oSMo::Db::UserPermissions;
6 7
7 8
has _settings 		=> undef;
8 9
has _permissions	=> undef;
9 10
10 11
sub setting {
11 12
	my ($self, $setting) = @_;
12
	my $sets = $self->_settings() || $self->settings;
13
	my $sets = $self->settings;
13 14
14 15
	return $sets->get($setting);
15 16
}
@ -25,7 +26,7 @@ sub settings {
25 26
26 27
	my $sets;
27 28
	unless ($sets = $self->_settings) {
28
		$sets = oSMo::Db::UserSettings->retrieve($self->id);
29
		$sets = oSMo::Db::UserSettings->retrieve( $self->id );
29 30
30 31
		$self->_settings($sets) if $sets;
31 32
	}
@ -35,11 +36,59 @@ sub settings {
35 36
36 37
#TODO: permissions
37 38
sub have_permission {
38
	return;
39
	my $self = shift;
40
41
	return $self->permissions->get(shift);
42
}
43
44
sub have_any_permission {
45
	my $self = shift;
46
47
	my $perms = $self->permissions;
48
	for my $p (@_) {
49
		return 1 if $perms->get($p);
50
	}
51
52
	return 0;
53
}
54
55
sub have_all_permissions {
56
	my $self = shift;
57
58
	my $perms = $self->permissions;
59
	for my $p (@_) {
60
		return 0 unless $perms->get($p);
61
	}
62
	return 1;
39 63
}
40 64
41 65
sub permissions {
66
	my $self = shift;
67
68
	my $perms;
69
	unless ($perms = $self->_permissions ) {
70
		$perms = oSMo::Db::UserPermissions->retrieve( $self->id );
71
72
		$self->_permissions($perms) if $perms;
73
	}
42 74
75
	return $perms;
43 76
}
44 77
78
sub add_permissions {
79
	my $self = shift;
80
81
	$self->permissions->set(@_);
82
}
83
*add_permission = *add_permissions;
84
85
sub del_permissions {
86
	my $self = shift;
87
88
	$self->permissions->delete(@_);
89
}
90
*del_permission = *del_permissions;
91
45 92
1;
93
__END__
94