UNPKG

4.44 kBtext/coffeescriptView Raw
1GoogleAPIAdminSDK = require "#{__dirname}/google_api_admin_sdk"
2qs = require 'qs'
3_ = require 'underscore'
4dotty = require 'dotty'
5utils = require "#{__dirname}/utils"
6sanitize = require 'sanitize-arguments'
7{Query} = require "#{__dirname}/query.coffee"
8crypto = require 'crypto'
9
10class UserProvisioning extends GoogleAPIAdminSDK
11 insert: (body, fields, cb) ->
12 arglist = sanitize arguments, UserProvisioning.insert, [Object, String, Function]
13 args = _.object ['body', 'fields', 'cb'], arglist
14 die = utils.die_fn args.cb
15 return die "UserProvisioning::insert expected (Object body, [callback])" if not args.body?
16 required = ['name.familyName', 'name.givenName', 'password', 'primaryEmail']
17 for r in required
18 if not dotty.exists(body, r)
19 return die "UserProvisioning::insert requires '#{r}'"
20 uri = "https://www.googleapis.com/admin/directory/v1/users"
21 shasum = crypto.createHash 'sha1'
22 shasum.update body.password
23 body.password = shasum.digest 'hex'
24 body.hashFunction = 'SHA-1'
25 opts =
26 method: 'post'
27 uri: uri
28 json: args.body
29 q = new Query @, opts
30 return q unless args.cb?
31 q.exec args.cb
32
33 patch: (userkey, body, fields, cb) ->
34 arglist = sanitize arguments, UserProvisioning.patch, [String, Object, String, Function]
35 args = _.object ['userkey', 'body', 'fields', 'cb'], arglist
36 die = utils.die_fn args.cb
37 if not args.userkey?
38 return die "UserProvisioning::patch expected (String userkey, [Object body, String fields, callback])"
39 uri = "https://www.googleapis.com/admin/directory/v1/users/#{args.userkey}"
40 uri += "?#{qs.stringify {fields: args.fields}}" if args.fields?
41 opts = { method: 'patch', uri: uri, json: (args.body or true) }
42 q = new Query @, opts
43 return q unless args.cb?
44 q.exec args.cb
45
46 update: (userkey, body, fields, cb) ->
47 arglist = sanitize arguments, UserProvisioning.patch, [String, Object, String, Function]
48 args = _.object ['userkey', 'body', 'fields', 'cb'], arglist
49 die = utils.die_fn args.cb
50 if not args.userkey?
51 return die "UserProvisioning::update expected (String userkey, [Object body, String fields, callback])"
52 uri = "https://www.googleapis.com/admin/directory/v1/users/#{args.userkey}"
53 uri += "?#{qs.stringify {fields: args.fields}}" if args.fields?
54 opts = { method: 'put', uri: uri, json: (args.body or true) }
55 q = new Query @, opts
56 return q unless args.cb?
57 q.exec args.cb
58
59 delete: (userkey, cb) =>
60 arglist = sanitize arguments, UserProvisioning.delete, [String, Function]
61 args = _.object ['userkey', 'cb'], arglist
62 die = utils.die_fn args.cb
63 return die "UserProvisioning::delete expected (String userkey, [callback])" if not args.userkey?
64 uri = "https://www.googleapis.com/admin/directory/v1/users/#{args.userkey}"
65 opts = { method: 'delete', uri: uri, json: true }
66 q = new Query @, opts
67 return q unless args.cb?
68 q.exec args.cb
69
70 # documentation at https://developers.google.com/admin-sdk/directory/v1/reference/users/list
71 # and https://developers.google.com/admin-sdk/directory/v1/reference/users/get
72 get: (userkey, cb) =>
73 # when requesting partial responses with the 'fields' param, you must request the nextPageToken field to enable pagination
74 arglist = sanitize arguments, UserProvisioning.get, [String, Function]
75 args = _.object ['userkey', 'cb'], arglist
76 die = utils.die_fn args.cb
77 return die "UserProvisioning::get requires (String userkey, [callback])" if not args.userkey?
78 uri = "https://www.googleapis.com/admin/directory/v1/users/#{args.userkey}"
79 opts = { json: true, uri: uri }
80 q = new Query @, opts
81 return q unless args.cb?
82 q.exec args.cb
83
84 list: (params, cb) =>
85 if params.fields? and not (_.str.include params.fields, 'nextPageToken')
86 params.fields = 'nextPageToken,' + params.fields
87 arglist = sanitize arguments, UserProvisioning.list, [Object, Function]
88 args = _.object ['params', 'cb'], arglist
89 die = utils.die_fn args.cb
90 return die "UserProvisioning::list requires (Object params, [callback])" if not args.params?
91 uri = "https://www.googleapis.com/admin/directory/v1/users"
92 uri += "?#{qs.stringify args.params}" if args.params
93 opts = { json: true, uri: uri }
94 q = new Query @, opts
95 return q unless args.cb?
96 q.exec args.cb
97
98module.exports = UserProvisioning