File: V2_0.pm

package info (click to toggle)
libvuser-google-api-perl 1.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 348 kB
  • sloc: perl: 3,176; makefile: 4
file content (758 lines) | stat: -rw-r--r-- 18,512 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
package VUser::Google::Provisioning::V2_0;
use warnings;
use strict;

our $VERSION = '0.2.0';

use Moose;
extends 'VUser::Google::Provisioning';

use VUser::Google::Provisioning::UserEntry;

has '+base_url' => (default => 'https://apps-apis.google.com/a/feeds/');

#### Methods ####
## Users
#
# %options
#   userName*
#   givenName*
#   familyName*
#   password*
#   hashFunctioName (SHA-1|MD5)
#   suspended       (bool)
#   quota           (in MB)
#   changePasswordAtNextLogin (bool)
#   admin           (bool)
sub CreateUser {
    my $self    = shift;

    my %options = ();

    if (ref $_[0]
	    and $_[0]->isa('VUser::Google::Provisioning::UserEntry')) {
	%options = $_[0]->as_hash;
    }
    else {
	%options = @_;
    }

    $self->google()->Login();
    my $url = $self->base_url.$self->google->domain.'/user/2.0';

    my $post = '<?xml version="1.0" encoding="UTF-8"?>
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:apps="http://schemas.google.com/apps/2006">
    <atom:category scheme="http://schemas.google.com/g/2005#kind" 
        term="http://schemas.google.com/apps/2006#user"/>
';

    ## login
    $post .= '<apps:login ';
    $post .= " userName=\"$options{'userName'}\"";

    $post .= " password=\""
	.$self->_escape_quotes($options{'password'})."\"";

    if ($options{hashFunctionName}) {
	$post .= " hashFunctionName=\"$options{hashFunctionName}\"";
    }

    if ($options{suspended}) {
	$post .= ' suspended="'.$self->_as_bool($options{suspended}).'"';
    }

    if ($options{changePasswordAtNextLogin}) {
	$post .= ' changePasswordAtNextLogin="'
	    .$self->_as_bool($options{changePasswordAtNextLogin}).'"';
    }

    if ($options{admin}) {
	$post .= ' admin="'.$self->_as_bool($options{admin}).'"';
    }

    $post .= '/>';

    ## quota
    if ($options{quota}) {
	$post .= "<apps:quota limit=\"$options{quota}\"/>";
    }

    ## name
    $post .= '<apps:name';
    $post .= " familyName=\"$options{familyName}\"";
    $post .= " givenName=\"$options{givenName}\"";
    $post .= '/>';

    $post .= '</atom:entry>';

    if ($self->google->Request('POST', $url, $post)) {
	## build UserEntry
	$self->dprint('Created user');
	my $entry = $self->_build_user_entry($self->google->result);
	return $entry;
    }
    else {
	## ERROR!
	$self->dprint('CreateUser failed: '.$self->google->result->{reason});
	die "Error creating user: ".$self->google->result->{'reason'}."\n";
    }
}

sub RetrieveUser {
    my $self = shift;
    my $username = shift;

    my $url = $self->base_url.$self->google->domain.'/user/2.0/'.$username;

    if ($self->google->Request('GET', $url)) {
	return $self->_build_user_entry($self->google->result);
    }
    else {
	if ($self->google->result->{'reason'} =~ /EntityDoesNotExist/) {
	    return undef;
	}
	else {
	    die "Error retrieving user: ".$self->google->result->{'reason'}."\n";
	}
    }
}

# Retrieve one page of users.
# How to return the next page?
# Returns (
#   entries => \@entries, # list of UserEntry objects
#   next    => $next      # the next username if another page exists
#                         # undef otherwise
#   )
sub RetrieveUsers {
    my $self       = shift;
    my $start_user = shift;

    my @entries = ();
    my $next_user;

    my $url = $self->base_url.$self->google->domain.'/user/2.0';
    if ($start_user) {
	$url .= "?startUsername=$start_user";
    }

    if ($self->google->Request('GET', $url)) {
	foreach my $entry (@{ $self->google->result->{'entry'} }) {
	    ## Create UserEntry object
	    my $user = $self->_build_user_entry($entry);
	    push @entries, $user;
	}
    }
    else {
	## There was an error
	die "Error fetching users: ".$self->google->result->{'reason'}."\n";
    }

    # Look for the a link tag that says there should be more results
    # A link tag with rel=next means there is another page
    foreach my $link (@{ $self->google->result->{'link'} }) {
	if ($link->{'rel'} eq 'next') {
	    $url = $link->{'href'};
	    if ($url =~ /startUsername=([^\"]+)/) {
		$next_user = $1;
	    }
	}
    }

    return ( entries => \@entries, next => $next_user );
}

# Alias for RetrieveUsers
sub RetrievePageOfUsers {
    $_[0]->RetrieveUsers(@_);
}

# Returns a list of UserEntry objects
sub RetrieveAllUsers {
    my $self = shift;

    my @entries = ();
    my $next;

    my %results;

    eval {
	%results = $self->RetrieveUsers;
	push @entries, @{ $results{'entries'} };
	$next = $results{'next'};
    };
    die $@ if $@;

    while ($next) {
	eval {
	    %results = $self->RetrieveUsers($next);
	    push @entries, @{ $results{'entries'} };
	    $next = $results{'next'};
	};
	die $@ if $@;
    }

    return @entries;
}

# %options
#   userName*
#   givenName
#   familyName
#   password
#   hashFunctioName (SHA-1|MD5)
#   suspended       (bool)
#   quota           (in MB)
#   changePasswordAtNextLogin (bool)
#   admin           (admin)
sub UpdateUser {
    my $self = shift;

    my %options = ();

    if (ref $_[0]
	    and $_[0]->isa('VUser::Google::Provisioning::UserEntry')) {
	%options = $_[0]->as_hash;
    }
    else {
	%options = @_;
    }

    die "Can't update user: userName not set\n" unless $options{'userName'};

    my $url = $self->base_url.$self->google->domain
	."/user/2.0/$options{userName}";

    my $post = '<?xml version="1.0" encoding="UTF-8"?>
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:apps="http://schemas.google.com/apps/2006">
    <atom:category scheme="http://schemas.google.com/g/2005#kind" 
        term="http://schemas.google.com/apps/2006#user"/>
';

    ## update user info (login tag)
    if ($options{password}
	    or defined $options{suspended}
            or defined $options{changePasswordAtNextLogin}
	    or defined $options{admin}
	) {
	$post .= '<apps:login';

	if (defined $options{password}) {
	    $post .= ' password="';
	    $post .= $self->_escape_quotes($options{'password'});
	    $post .= '"';

	    if (defined $options{hashFunctionName}) {
		$post .= ' hashFunctionName="';
		$post .= $options{hashFunctionName};
		$post .= '"';
	    }
	}

	if (defined $options{suspended}) {
	    $post .= ' suspended="'.$self->_as_bool($options{suspended}).'"';
	}

	if (defined $options{changePasswordAtNextLogin}) {
	    $post .= ' changePasswordAtNextLogin="'
		.$self->_as_bool($options{changePasswordAtNextLogin}).'"';
	}

	if (defined $options{admin}) {
	    $post .= ' admin="'.$self->_as_bool($options{admin}).'"';
	}

	$post .= '/>';
    }

    ## Quota
    if ($options{quota}) {
	$post .= "<apps:quota limit=\"$options{quota}\"/>";
    }

    ## Name
    if ($options{givenName} or $options{familyName}) {
	$post .= '<apps:name';
	$post .= " familyName=\"$options{familyName}\"" if $options{familyName};
	$post .= " givenName=\"$options{givenName}\"" if $options{givenName};
	$post .= '/>';
    }

    $post .= '</atom:entry>';

    if ($self->google->Request('PUT', $url, $post)) {
	$self->dprint('Updated user');
	my $entry = $self->_build_user_entry($self->google->result);
	return $entry;
    }
    else {
	die "Error updating user: ".$self->google->result->{'reason'}."\n";
    }
}

sub RenameUser {
    my $self    = shift;
    my $oldname = shift;
    my $newname = shift;

    die "Can't rename user: old userName not set\n" unless $oldname;
    die "Can't rename user: new userName not set\n" unless $newname;

    my $url = $self->base_url.$self->google->domain
	."/user/2.0/$oldname";

    my $user = $self->RetrieveUser($oldname)
	or die "Unknown user: $oldname\n";

    my $post = '<?xml version="1.0" encoding="UTF-8"?>
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:apps="http://schemas.google.com/apps/2006">
    <atom:category scheme="http://schemas.google.com/g/2005#kind" 
        term="http://schemas.google.com/apps/2006#user"/>
';

    $post .= '<atom:title type="text">$oldname</atom:title>';
    $post .= '<atom:link rel="self" type="application/atom+xml"';
    $post .= " href=\"".$self->base_url.
	$self->google->domain."/user/2.0/$oldname\"/>";
    $post .= '<atom:link rel="edit" type="application/atom+xml"';
    $post .= " href=\"".$self->base_url.
	$self->google->domain."/user/2.0/$oldname\"/>";

    $post .= "<apps:login";
    $post .= " userName='$newname'";
    $post .= ' suspended="'.$self->_as_bool($user->Suspended).'"';
    $post .= ' admin="'.$self->_as_bool($user->Admin).'"';
    $post .= ' changePasswordAtNextLogin="'
	.$self->_as_bool($user->ChangePasswordAtNextLogin).'"';
    # $post .= ' agreedToTerms="'.$self->_as_bool($user->AgreedToTerms).'"';
    $post .= "/>";

    $post .= '</atom:entry>';

    if ($self->google->Request('PUT', $url, $post)) {
	$self->dprint("Renamed $oldname to $newname");
	my $entry = $self->_build_user_entry($self->google->result);
	return $entry;
    }
    else {
	die "Error rename user: ".$self->google->result->{'reason'}."\n";
    }
}

sub DeleteUser {
    my $self = shift;
    my $user;

    if (ref $_[0] and $_[0]->isa('VUser::Google::Provisioning::UserEntry')) {
	$user = $_[0]->UserName
    }
    else {
	$user = $_[0];
    }

    my $url = $self->base_url.$self->google->domain.'/user/2.0/'.$user;

    if ($self->google->Request('DELETE', $url)) {
	return 1;
    }
    else {
	return undef;
    }
}

sub ChangePassword {
    my $self          = shift;
    my $username      = shift;
    my $password      = shift;
    my $hash_function = shift;

    if (not $username or not $password) {
	die "Can't change password: username or password not set.\n";
    }

    my $entry = $self->UpdateUser(
	userName         => $username,
	password         => $password,
	hashFunctionName => $hash_function,
    );

    return $entry;
}

## Nicknames
sub CreateNickname {
}

sub RetrieveNickname {
}

sub RetrieveAllNicknamesForUser {
}

sub RetrieveAllNicknamesInDomain {
}

sub DeleteNickname {
}

# Takes the parsed XML object
sub _build_user_entry {
    my $self = shift;
    my $xml  = shift;

    my $entry = VUser::Google::Provisioning::UserEntry->new();

    $entry->UserName($xml->{'apps:login'}[0]{'userName'});

    if ($xml->{'apps:login'}[0]{'suspended'}) {
	if ($xml->{'apps:login'}[0]{'suspended'} eq 'true') {
	    $entry->Suspended(1);
	}
	else {
	    $entry->Suspended(0);
	}
    }

    if ($xml->{'apps:login'}[0]{'changePasswordAtNextLogin'}) {
	if ($xml->{'apps:login'}[0]{'changePasswordAtNextLogin'} eq 'true') {
	    $entry->ChangePasswordAtNextLogin(1);
	}
	else {
	    $entry->ChangePasswordAtNextLogin(0);
	}
    }

    if ($xml->{'apps:login'}[0]{'admin'}) {
	if ($xml->{'apps:login'}[0]{'admin'} eq 'true') {
	    $entry->Admin(1);
	}
	else {
	    $entry->Admin(0);
	}
    }

    if ($xml->{'apps:login'}[0]{'agreedToTerms'}) {
	if ($xml->{'apps:login'}[0]{'agreedToTerms'} eq 'true') {
	    $entry->AgreedToTerms(1);
	}
	else {
	    $entry->AgreedToTerms(0);
	}
    }

    $entry->FamilyName($xml->{'apps:name'}[0]{'familyName'});
    $entry->GivenName($xml->{'apps:name'}[0]{'givenName'});
    $entry->Quota($xml->{'apps:quota'}[0]{'limit'});

    return $entry;
}

no Moose;
__PACKAGE__->meta->make_immutable;

1;

__END__

=head1 NAME

VUser::Google::Provisioning::V2_0 - Support for version 2.0 of the Google Provisioning API

=head1 SYNOPSIS

 use VUser::Google::ApiProtocol::V2_0;
 use VUser::Google::Provisioning::V2_0;
 
 my $google = VUser::Google::ApiProtocol::V2_0->new(
     domain   => 'example.com',
     admin    => 'admin_user',
     password => 'secret',
 );
 
 my $api = VUser::Google::Provisioning::V2_0->new(
     google => $google,
 );
 
 ## Create user
 my $new_user = $api->CreateUser(
     userName    => 'fflintstone',
     givenName   => 'Fred',
     familyName  => 'Flintstone',
     password    => 'I<3Wilma',
 );
 
 ## Retrieve a user
 my $user = $api->RetrieveUser('fflintstone');
 
 ## Retrieve all userr
 my @users = $api->RetrieveAllUsers();
 
 ## Update a user
 my $updated = $api->UpdateUser(
     userName   => 'fflintstone',
     givenName  => 'Fredrock',
     familyName => 'FlintStone',
     suspended  => 1,
     quota      => 2048,
 );
 
 ## Change password
 $updated = $api->ChangePassword('fflintstone', 'new-pass');
 
 $updated = $api->ChangePassword(
     'fflintstone',
     '51eea05d46317fadd5cad6787a8f562be90b4446',
     'SHA-1',
 );
 
 $updated = $api->ChangePassword(
     'fflintstone',
     'd27117a019717502efe307d110f5eb3d',
     'MD5',
 );
 
 ## Delete a user
 my $rc = $api->DeleteUser('fflintstone');

=head1 DESCRIPTION

VUser::Google::Provisioning::V2_0 provides support for managing users
using version 2.0 of the Google Provisioning API.

In order to use the Google Provisioning API, you must turn on API support
from the Google Apps for Your Domain control panel. The user that is
used to create the VUser::Google::ApiProtocol object must have administrative
privileges on the domain.

B<Note:> It's a good idea to log into the web control panel at least once
as the API user in order to accept the the terms of service and admin terms.
If you don't, you'll get intermittent authentication errors when trying to
use the API.

=head2 METHODS

Unless stated otherwise, these methods will die() if there is an API error.

=head3 CreateUser

CreateUser() takes a hash of create options and returns a
VUser::Google::Provisioning::UserEntry object if the account
was created. CreateUser() will die() if there is an error.

The keys of the hash are:

=over

=item userName (required)

The user name of the account to create

=item givenName (required)

The user's given name

=item familyName (required)

The user's family name

=item password (required)

The user's password. If hashFunctionName is also set, this is
the base16-encoded hash of the password. Otherwise, this is the
user's plaintext password.

Google required that passwords be, at least, six characters.

=item hashFunctionName

hashFunctionName must be I<SHA-1> or I<MD5>. If this is set,
password is the base16-encoded password hash.

=item quota

The user's quota in MB.

Not all domains will be allowed to set users'
quotas. If that's the case, creation will still succeed but the
quota will be set to the default for your domain.

=item changePasswordAtNextLogin

If set to a true value, e.g. C<1>, the user will be required to
change their password the next time they login in. This is the default.
You may turn this off by setting changePasswordAtNextLogin to C<0>.

=item admin

If set to a true value, e.g. C<1>, the user will be granted
administrative privileges. A false value, e.g. C<0>, admin rights will
be revoked. By default, users will not be granted admin rights.

=back

=head3 RetrieveUser

 my $user = $api->RetrieveUser('fflintstone');

Retrieves a specified user by the user name. RetieveUser will return a
VUser::Google::Provisioning::UserEntry if the user exists and undef
if it doesn't.

=head3 RetrieveUsers

 my @users = ();
 
 my %results = $api->RetrieveUsers();
 @users = @{ $results{entries} };
 
 while ($results{next}) {
     %results = $api->RetrieveUsers($results{next});
     push @users, @{ $results{entries} };
 }

Fetches one page of users starting at a given user name. Currently,
a page is defined as 100 users. This is useful if you plan on
paginating the results yourself or if you have a very large number
of users.

The returned result is a hash with the following keys:

=over

=item entries

A list reference containing the user accounts. Each entry
is a VUser::Google::Provisioning::UserEntry object.

=item next

The user name for the start of the next page. This will be
undefined (C<undef>) if there are no more pages.

=back

See RetrieveAllUsers() if you want
to fetch all of the accounts at once.

=head3 RetrievePageOfUsers

This is a synonym for RetrieveUsers()

=head3 RetrieveAllUsers

 my @users = $api->RetrieveAllUsers();

Get a list of all the users for the domain. The entries in the
list are VUser::Google::Provisioning::UserEntry objects.

=head3 UpdateUser

 my $updated = $api->UpdateUser(
     userName  => 'fflintstone',
     givenName => 'Fred',
     # ... other options
 );

Updates an account. UpdateUser takes the same options as CreateUser() but
only userName is required.

UpdateUser() cannot be used to rename an account. See RenameUser().

=head3 RenameUser

 my $user_user = $api->RenameUser($oldname, $newname);

Rename an account. The first parameter is the old user name; the
second is the new user name. RenameUser() will die if the old name
does not exist.

=head3 DeleteUser

 my $rc = $api->DeleteUser('fflintstone');

Deletes a given user. Returns true if the delete succeded and dies
if there was an error.

=head3 ChangePassword

 $updated = $api->ChangePassword('fflintstone', 'new-pass');
 
 $updated = $api->ChangePassword(
     'fflintstone',
     '51eea05d46317fadd5cad6787a8f562be90b4446',
     'SHA-1',
 );
 
 $updated = $api->ChangePassword(
     'fflintstone',
     'd27117a019717502efe307d110f5eb3d',
     'MD5',
 );

Change a users password.

ChangePassword takes the user name, password and, optionally, a
hash function name. If the hash function name is set, the password,
is the base16-encoded password, otherwise it is the clear text password.

Accepted values for the has function name are I<MD5> and I<SHA-1>.

There is no difference between using this and using UpdateUser to change
the user's password.

=head1 SEE ALSO

=over

=item *

VUser::Google::Provisioning

=item *

VUser::Google::ApiProtocol::V2_0

=item *

VUser::Google::EmailSettings::V2_0

=item *

http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_developers_protocol.html

item *

http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_reference.html

=back

=head1 BUGS

Bugs may be reported at http://code.google.com/p/vuser/issues/list.

=head1 AUTHOR

Randy Smith <perlstalker@vuser.org>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2009  Randall Smith

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

=cut