annotate Lib/ObjectStore/CDBI/Users.pm @ 30:dd4d72600c69

ORM in works
author Sergey
date Tue, 03 Nov 2009 16:31:47 +0300
parents 03e58a454b20
children 16ada169ca75
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
1 #!/usr/bin/perl -w
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
2 use strict;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
3
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
4 package ObjectStore::CDBI::Users;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
5 use Common;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
6 use Digest::MD5 qw(md5_hex);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
7 our @ISA = qw(Object);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
8
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
9 our $Namespace;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
10 our $DataModule;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
11
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
12 our $Prefix = $Namespace ? $Namespace.'::' : '';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
13
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
14 if ($DataModule) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
15 $DataModule =~ s/::/\//g;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
16 $DataModule .= '.pm';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
17 require $DataModule;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
18 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
19
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
20 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
21 DeclareProperty DSNamespace => ACCESS_NONE;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
22 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
23
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
24 sub CTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
25 my ($this,%args) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
26
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
27 $this->{$DSNamespace} = $args{'DSNamespace'};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
28 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
29
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
30 sub ClassName {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
31 return $_[0]->{$DSNamespace} ? $_[0]->{$DSNamespace}. $_[1] : $_[1];
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
32 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
33
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
34 sub FindUser {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
35 my ($this,$uname) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
36
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
37 my @Users = $this->ClassName('Principal')->search(Name => $uname);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
38 return shift @Users;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
39 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
40
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
41 sub CreateUser {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
42 my ($this,$uname,$description,$active) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
43
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
44 if (my $user = $this->FindUser($uname)) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
45 die new Exception("The user is already exists",$uname);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
46 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
47 return $this->ClassName('Principal')->insert({Name => $uname, Description => $description, Active => $active});
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
48 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
49 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
50
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
51 sub DeleteUser {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
52 my ($this,$objUser) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
53
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
54 $objUser->delete;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
55 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
56
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
57 sub GetUserAuthData {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
58 my ($this,$objUser,$objSecPackage) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
59
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
60 my @Data = $this->ClassName('AuthData')->search(User => $objUser,Package => $objSecPackage->Name);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
61 return $Data[0];
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
62 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
63
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
64 sub SetUserAuthData {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
65 my ($this,$objUser,$objSecPackage,$objAuthData) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
66
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
67 if (my $AuthData = $this->GetUserAuthData($objUser,$objSecPackage)) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
68 $AuthData->AuthData(objAuthData->SessionAuthData);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
69 $AuthData->update;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
70 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
71 $this->ClassName('AuthData')->insert({ User => $objUser, Package => $objSecPackage->Name, AuthData => $objAuthData->SessionAuthData});
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
72 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
73 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
74
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
75 sub CreateSession {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
76 my ($this,$SSID,$objUser,$objAuthData) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
77
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
78 my $session = $this->ClassName('Session')->insert({SSID => $SSID, User => $objUser, SecData => $objAuthData->SessionAuthData, LastUsage => DateTime->now() });
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
79 $session->autoupdate(1);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
80 return $session;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
81 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
82
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
83 sub CloseSession {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
84 my ($this,$objSession) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
85
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
86 $objSession->delete;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
87 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
88
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
89 sub LoadSession {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
90 my ($this,$SSID) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
91 my @Data = $this->ClassName('Session')->search(SSID => $SSID);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
92 if ($Data[0]) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
93 $Data[0]->autoupdate(1);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
94 return $Data[0];
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
95 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
96 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
97
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
98 sub construct {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
99 return __PACKAGE__->new(DSNamespace => $Prefix);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
100 }