本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 line
2.9KB

  1. package couchbase
  2. import (
  3. "bytes"
  4. "fmt"
  5. )
  6. type User struct {
  7. Name string
  8. Id string
  9. Domain string
  10. Roles []Role
  11. }
  12. type Role struct {
  13. Role string
  14. BucketName string `json:"bucket_name"`
  15. }
  16. // Sample:
  17. // {"role":"admin","name":"Admin","desc":"Can manage ALL cluster features including security.","ce":true}
  18. // {"role":"query_select","bucket_name":"*","name":"Query Select","desc":"Can execute SELECT statement on bucket to retrieve data"}
  19. type RoleDescription struct {
  20. Role string
  21. Name string
  22. Desc string
  23. Ce bool
  24. BucketName string `json:"bucket_name"`
  25. }
  26. // Return user-role data, as parsed JSON.
  27. // Sample:
  28. // [{"id":"ivanivanov","name":"Ivan Ivanov","roles":[{"role":"cluster_admin"},{"bucket_name":"default","role":"bucket_admin"}]},
  29. // {"id":"petrpetrov","name":"Petr Petrov","roles":[{"role":"replication_admin"}]}]
  30. func (c *Client) GetUserRoles() ([]interface{}, error) {
  31. ret := make([]interface{}, 0, 1)
  32. err := c.parseURLResponse("/settings/rbac/users", &ret)
  33. if err != nil {
  34. return nil, err
  35. }
  36. // Get the configured administrator.
  37. // Expected result: {"port":8091,"username":"Administrator"}
  38. adminInfo := make(map[string]interface{}, 2)
  39. err = c.parseURLResponse("/settings/web", &adminInfo)
  40. if err != nil {
  41. return nil, err
  42. }
  43. // Create a special entry for the configured administrator.
  44. adminResult := map[string]interface{}{
  45. "name": adminInfo["username"],
  46. "id": adminInfo["username"],
  47. "domain": "ns_server",
  48. "roles": []interface{}{
  49. map[string]interface{}{
  50. "role": "admin",
  51. },
  52. },
  53. }
  54. // Add the configured administrator to the list of results.
  55. ret = append(ret, adminResult)
  56. return ret, nil
  57. }
  58. func (c *Client) GetUserInfoAll() ([]User, error) {
  59. ret := make([]User, 0, 16)
  60. err := c.parseURLResponse("/settings/rbac/users", &ret)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return ret, nil
  65. }
  66. func rolesToParamFormat(roles []Role) string {
  67. var buffer bytes.Buffer
  68. for i, role := range roles {
  69. if i > 0 {
  70. buffer.WriteString(",")
  71. }
  72. buffer.WriteString(role.Role)
  73. if role.BucketName != "" {
  74. buffer.WriteString("[")
  75. buffer.WriteString(role.BucketName)
  76. buffer.WriteString("]")
  77. }
  78. }
  79. return buffer.String()
  80. }
  81. func (c *Client) PutUserInfo(u *User) error {
  82. params := map[string]interface{}{
  83. "name": u.Name,
  84. "roles": rolesToParamFormat(u.Roles),
  85. }
  86. var target string
  87. switch u.Domain {
  88. case "external":
  89. target = "/settings/rbac/users/" + u.Id
  90. case "local":
  91. target = "/settings/rbac/users/local/" + u.Id
  92. default:
  93. return fmt.Errorf("Unknown user type: %s", u.Domain)
  94. }
  95. var ret string // PUT returns an empty string. We ignore it.
  96. err := c.parsePutURLResponse(target, params, &ret)
  97. return err
  98. }
  99. func (c *Client) GetRolesAll() ([]RoleDescription, error) {
  100. ret := make([]RoleDescription, 0, 32)
  101. err := c.parseURLResponse("/settings/rbac/roles", &ret)
  102. if err != nil {
  103. return nil, err
  104. }
  105. return ret, nil
  106. }
上海开阖软件有限公司 沪ICP备12045867号-1