Class: AuthingRuby::UsersManagementClient

Inherits:
Object
  • Object
show all
Defined in:
lib/authing_ruby/management/UsersManagementClient.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, graphqlClient = nil, httpClient = nil, tokenProvider = nil, publicKeyManager = nil) ⇒ UsersManagementClient

Returns a new instance of UsersManagementClient.



7
8
9
10
11
12
13
# File 'lib/authing_ruby/management/UsersManagementClient.rb', line 7

def initialize(options = {}, graphqlClient = nil, httpClient = nil, tokenProvider = nil, publicKeyManager = nil)
  @options = options
  @graphqlClient = graphqlClient
  @httpClient = httpClient
  @tokenProvider = tokenProvider
  @publicKeyManager = publicKeyManager
end

Instance Method Details

#batchObject

TODO 批量获取用户



113
114
# File 'lib/authing_ruby/management/UsersManagementClient.rb', line 113

def batch
end

#create(userInfo = {}, options = {}) ⇒ Object

创建用户



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/authing_ruby/management/UsersManagementClient.rb', line 16

def create(userInfo = {}, options = {})
  keepPassword = options.fetch(:keepPassword, false)
  password = userInfo.fetch(:password, nil)
  # 先对密码进行处理
  if password
    publicKey = @publicKeyManager.getPublicKey()
    encryptedPassword = AuthingRuby::Utils.encrypt(password, publicKey)
    userInfo[:password] = encryptedPassword
  end

  # 然后再发请求
  graphqlAPI = AuthingRuby::GraphQLAPI.new
  variables = {
    "userInfo": userInfo,
    "keepPassword": keepPassword,
  }
  hash = graphqlAPI.createUser(@graphqlClient, @tokenProvider, variables)
  user = hash.dig("data", "createUser")
  return user if user
  return hash
end

#delete(user_id) ⇒ Object

删除用户



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/authing_ruby/management/UsersManagementClient.rb', line 73

def delete(user_id)
  variables = {
    "id": user_id,
  }
  graphqlAPI = AuthingRuby::GraphQLAPI.new
  hash = graphqlAPI.deleteUser(@graphqlClient, @tokenProvider, variables)
  # {"data":{"deleteUser":{"message":"删除成功!","code":200}}}
  data = hash.dig("data", "deleteUser")
  return data if data
  return hash
end

#deleteMany(user_ids = []) ⇒ Object

批量删除用户



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/authing_ruby/management/UsersManagementClient.rb', line 86

def deleteMany(user_ids = [])
  variables = {
    "ids": user_ids,
  }
  graphqlAPI = AuthingRuby::GraphQLAPI.new
  hash = graphqlAPI.deleteUsers(@graphqlClient, @tokenProvider, variables)
  # {"data":{"deleteUsers":{"message":"删除成功!","code":200}}}
  data = hash.dig("data", "deleteUsers")
  return data if data
  return hash
end

#detail(user_id) ⇒ Object

通过 ID 获取用户信息



61
62
63
64
65
66
67
68
69
70
# File 'lib/authing_ruby/management/UsersManagementClient.rb', line 61

def detail(user_id)
  graphqlAPI = AuthingRuby::GraphQLAPI.new
  variables = {
    "id": user_id,
  }
  hash = graphqlAPI.user(@graphqlClient, @tokenProvider, variables)
  user = hash.dig("data", "user")
  return user if user
  return hash
end

#exists(options = {}) ⇒ Object

检查用户是否存在



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/authing_ruby/management/UsersManagementClient.rb', line 117

def exists(options = {})
  username = options.fetch(:username, nil)
  email = options.fetch(:email, nil)
  phone = options.fetch(:phone, nil)
  if username == nil && email == nil && phone == nil
    throw "缺少参数, 请至少传入一个选项: username, email, phone"
  end

  variables = {
    "username": username,
    "email": email,
    "phone": phone,
  }
  graphqlAPI = AuthingRuby::GraphQLAPI.new
  hash = graphqlAPI.isUserExists(@graphqlClient, @tokenProvider, variables)
  data = hash.dig("data", "isUserExists")
  return data if data
  return hash
end

#find(options = {}) ⇒ Object

查找用户



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/authing_ruby/management/UsersManagementClient.rb', line 138

def find(options = {})
  username = options.fetch(:username, nil)
  email = options.fetch(:email, nil)
  phone = options.fetch(:phone, nil)
  externalId = options.fetch(:externalId, nil)
  if username == nil && email == nil && phone == nil && externalId == nil
    throw "缺少参数, 请至少传入一个选项: username, email, phone, externalId"
  end

  variables = {
    "username": username,
    "email": email,
    "phone": phone,
    "externalId": externalId,
  }
  graphqlAPI = AuthingRuby::GraphQLAPI.new
  hash = graphqlAPI.findUser(@graphqlClient, @tokenProvider, variables)
  data = hash.dig("data", "findUser")
  return data if data
  return hash
end

#list(page = 1, limit = 10) ⇒ Object

获取用户列表



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/authing_ruby/management/UsersManagementClient.rb', line 99

def list(page = 1, limit = 10)
  variables = {
    "page": page,
    "limit": limit,
  }
  graphqlAPI = AuthingRuby::GraphQLAPI.new
  hash = graphqlAPI.users(@graphqlClient, @tokenProvider, variables)
  data = hash.dig("data", "users")
  return data if data
  return hash
end

#listRoles(user_id) ⇒ Object



171
172
# File 'lib/authing_ruby/management/UsersManagementClient.rb', line 171

def listRoles(user_id)
end

#refreshTokenObject

TODO



165
166
# File 'lib/authing_ruby/management/UsersManagementClient.rb', line 165

def refreshToken
end

#searchObject

TODO



161
162
# File 'lib/authing_ruby/management/UsersManagementClient.rb', line 161

def search
end

#update(id, updates = {}) ⇒ Object

修改用户资料



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/authing_ruby/management/UsersManagementClient.rb', line 39

def update(id, updates = {})
  # 预处理密码(如果有的话)
  password = updates.fetch(:password, nil)
  if password
    publicKey = @publicKeyManager.getPublicKey()
    encryptedPassword = AuthingRuby::Utils.encrypt(password, publicKey)
    updates[:password] = encryptedPassword
  end

  # 然后再发请求
  graphqlAPI = AuthingRuby::GraphQLAPI.new
  variables = {
    "id": id,
    "input": updates
  }
  hash = graphqlAPI.updateUser(@graphqlClient, @tokenProvider, variables)
  user = hash.dig("data", "updateUser")
  return user if user
  return hash
end