|
@@ -0,0 +1,64 @@
|
|
1
|
+use LWP::Simple;
|
|
2
|
+use NRT::Test;
|
|
3
|
+use Crypt::JWT(decode_jwt);
|
|
4
|
+
|
|
5
|
+package User;
|
|
6
|
+
|
|
7
|
+sub new {
|
|
8
|
+ my ($classe, $email, $username, $password, $manager) = @_;
|
|
9
|
+ my $this = {};
|
|
10
|
+ $classe = ref($classe) || $classe;
|
|
11
|
+ bless ( $this, $classe );
|
|
12
|
+ $this->{email} = $email;
|
|
13
|
+ $this->{username} = $username;
|
|
14
|
+ $this->{password} = $password;
|
|
15
|
+ $this->{manager} = $manager;
|
|
16
|
+ $this->{token} = "";
|
|
17
|
+ $this->{id} = "";
|
|
18
|
+ return $this;
|
|
19
|
+}
|
|
20
|
+
|
|
21
|
+sub authentification {
|
|
22
|
+ my $this = shift ;
|
|
23
|
+ my $data = "{\"user\" : {\"email\" : \"".$this->{email}."\", \"password\" : \"".$this->{password}."\"}}";
|
|
24
|
+ $rq = $this->{manager}->rq( "tokens", "POST", $data ); ;
|
|
25
|
+ $this->{token} = $rq->{token} ;
|
|
26
|
+ return $rq;
|
|
27
|
+}
|
|
28
|
+
|
|
29
|
+sub create {
|
|
30
|
+ my ($this) = shift ;
|
|
31
|
+ my $data = "{\"user\" : {\"username\" : \"". $this->{username} ."\", \"email\" : \"".$this->{email}."\", \"password\" : \"".$this->{password}."\"}}";
|
|
32
|
+ $rq = $this->{manager}->rq( "users", "POST", "$data" );
|
|
33
|
+ $this->{id} = $rq->{data}{id};
|
|
34
|
+ return $rq;
|
|
35
|
+}
|
|
36
|
+
|
|
37
|
+sub remove {
|
|
38
|
+ my ($this) = @_ ;
|
|
39
|
+ my $data = "{\"user\" : {\"username\" : \"". $this->{username} ."\", \"email\" : \"".$this->{email}."\", \"password\" : \"".$this->{password}."\"}}";
|
|
40
|
+ my $user_id = Crypt::JWT::decode_jwt(token => $this->get_token, ignore_signature => 1)->{user_id};
|
|
41
|
+ return $this->{manager}->rq( "users/".$user_id, "DELETE", $data, $this->get_token );
|
|
42
|
+}
|
|
43
|
+
|
|
44
|
+sub fetch {
|
|
45
|
+ my ($this) = @_ ;
|
|
46
|
+ $token = $this->get_token;
|
|
47
|
+ if( !$token ){
|
|
48
|
+ return 0;
|
|
49
|
+ }
|
|
50
|
+ my $user_id = Crypt::JWT::decode_jwt(token => $token, ignore_signature => 1)->{user_id};
|
|
51
|
+ return $this->{manager}->rq( "users/".$user_id, "GET", "", $this->get_token );
|
|
52
|
+}
|
|
53
|
+
|
|
54
|
+sub get_token {
|
|
55
|
+ my $this = shift;
|
|
56
|
+ if ($this->{token} eq ""){
|
|
57
|
+ $this->authentification;
|
|
58
|
+ }
|
|
59
|
+ return (exists $this->{token})?
|
|
60
|
+ $this->{token} :
|
|
61
|
+ 0 ;
|
|
62
|
+}
|
|
63
|
+
|
|
64
|
+return 1;
|