crowd

package module
v3.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

PkgGoDev GoReportCard GitHub Actions CI Status GitHub Actions CodeQL Status Codebeat badge

Usage exampleCI StatusLicense


go-crowd is a Go package for working with Crowd REST API.

[!IMPORTANT] Please note that this package only supports retrieving data from the Crowd API (i.e. you cannot create or modify data with this package).

Usage example

package main

import (
  "fmt"
  "github.com/essentialkaos/go-crowd/v3"
)

func main() {
  api, err := crowd.NewAPI("https://crowd.domain.com/crowd/", "myapp", "MySuppaPAssWOrd")

  if err != nil {
    fmt.Printf("Error: %v\n", err)
    return
  }

  api.SetUserAgent("MyApp", "1.2.3")

  user, err := api.GetUser("john", true)

  if err != nil {
    fmt.Printf("Error: %v\n", err)
    return
  }

  fmt.Printf("%#v\n", user)
}

CI Status

Branch Status
master (Stable) CI
develop (Unstable) CI

License

Apache License, Version 2.0

Documentation

Index

Examples

Constants

View Source
const (
	GROUP_DIRECT = "direct"
	GROUP_NESTED = "nested"
)

Groups types

Variables

View Source
var (
	ErrInitEmptyURL      = errors.New("URL can't be empty")
	ErrInitEmptyApp      = errors.New("App can't be empty")
	ErrInitEmptyPassword = errors.New("Password can't be empty")
	ErrNoPerms           = errors.New("Application does not have permission to use Crowd")
	ErrUserNoFound       = errors.New("User could not be found")
	ErrGroupNoFound      = errors.New("Group could not be found")
)

API errors

Functions

func SimplifyAttributes

func SimplifyAttributes(attrs Attributes) map[string]string

SimplifyAttributes converts slice with attributes to map name->value

Example
attrs := Attributes{
	&Attribute{Name: "test", Values: []string{"1", "2"}},
	&Attribute{Name: "abcd", Values: []string{"test", "100"}},
}

attrsMap := SimplifyAttributes(attrs)

fmt.Println(attrsMap["test"])
Output:

1 2

Types

type API

type API struct {
	Client *fasthttp.Client // Client is client for http requests
	// contains filtered or unexported fields
}

API is Confluence API struct

func NewAPI

func NewAPI(url, app, password string) (*API, error)

NewAPI creates new API struct

Example
api, err := NewAPI("https://crowd.domain.com/crowd/", "myapp", "MySuppaPAssWOrd")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

api.SetUserAgent("MyApp", "1.2.3")

user, err := api.GetUser("john", true)

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

fmt.Printf("%#v\n", user)
Output:

func (*API) DeleteGroupAttributes

func (api *API) DeleteGroupAttributes(groupName, attrName string) error

DeleteGroupAttributes deletes a group attribute

func (*API) DeleteUserAttributes

func (api *API) DeleteUserAttributes(userName, attrName string) error

DeleteUserAttributes deletes a user attribute

func (*API) GetGroup

func (api *API) GetGroup(groupName string, withAttributes bool) (*Group, error)

GetGroup returns a group

Example
api, err := NewAPI("https://crowd.domain.com/crowd/", "myapp", "MySuppaPAssWOrd")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

group, err := api.GetGroup("my_group", true)

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

fmt.Printf("%#v\n", group)
Output:

func (*API) GetGroupAttributes

func (api *API) GetGroupAttributes(groupName string) (Attributes, error)

GetGroupAttributes returns a list of group attributes

Example
api, err := NewAPI("https://crowd.domain.com/crowd/", "myapp", "MySuppaPAssWOrd")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

attrs, err := api.GetGroupAttributes("my_group")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

attrsMap := SimplifyAttributes(attrs)

fmt.Printf("%#v\n", attrsMap)
Output:

func (*API) GetGroupDirectUsers

func (api *API) GetGroupDirectUsers(groupName string, options ...ListingOptions) ([]*User, error)

GetGroupDirectUsers returns the users that are direct members of the specified group

Example
api, err := NewAPI("https://crowd.domain.com/crowd/", "myapp", "MySuppaPAssWOrd")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

users, err := api.GetGroupDirectUsers("my_group")
// with listing options
users, err = api.GetGroupDirectUsers(
	"my_group",
	ListingOptions{StartIndex: 100, MaxResults: 50},
)

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

if len(users) > 0 {
	for _, user := range users {
		fmt.Printf("%#v\n", user)
	}
}
Output:

func (*API) GetGroupNestedUsers

func (api *API) GetGroupNestedUsers(groupName string, options ...ListingOptions) ([]*User, error)

GetGroupNestedUsers returns the users that are nested members of the specified group

Example
api, err := NewAPI("https://crowd.domain.com/crowd/", "myapp", "MySuppaPAssWOrd")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

users, err := api.GetGroupNestedUsers("my_group")
// with listing options
users, err = api.GetGroupNestedUsers(
	"my_group",
	ListingOptions{StartIndex: 100, MaxResults: 50},
)

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

if len(users) > 0 {
	for _, user := range users {
		fmt.Printf("%#v\n", user)
	}
}
Output:

func (*API) GetGroupUsers

func (api *API) GetGroupUsers(groupName, groupType string, options ...ListingOptions) ([]*User, error)

GetGroupUsers returns the users that are members of the specified group

Example
api, err := NewAPI("https://crowd.domain.com/crowd/", "myapp", "MySuppaPAssWOrd")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

users, err := api.GetGroupUsers("my_group", GROUP_DIRECT)
// with listing options
users, err = api.GetGroupUsers(
	"my_group", GROUP_DIRECT,
	ListingOptions{StartIndex: 100, MaxResults: 50},
)

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

if len(users) > 0 {
	for _, user := range users {
		fmt.Printf("%#v\n", user)
	}
}
Output:

func (*API) GetMemberships

func (api *API) GetMemberships() ([]*Membership, error)

GetMemberships returns full details of all group memberships, with users and nested groups

Example
api, err := NewAPI("https://crowd.domain.com/crowd/", "myapp", "MySuppaPAssWOrd")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

membership, err := api.GetMemberships()

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

for _, group := range membership {
	fmt.Printf("Group: %v\n", group.Group)

	for _, userInfo := range group.Users {
		fmt.Printf(" - %v\n", userInfo.Name)
	}

	fmt.Println("")
}
Output:

func (*API) GetUser

func (api *API) GetUser(userName string, withAttributes bool) (*User, error)

GetUser returns a user

Example
api, err := NewAPI("https://crowd.domain.com/crowd/", "myapp", "MySuppaPAssWOrd")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

user, err := api.GetUser("john", true)

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

fmt.Printf("%#v\n", user)
Output:

func (*API) GetUserAttributes

func (api *API) GetUserAttributes(userName string) (Attributes, error)

GetUserAttributes returns a list of user attributes

Example
api, err := NewAPI("https://crowd.domain.com/crowd/", "myapp", "MySuppaPAssWOrd")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

attrs, err := api.GetUserAttributes("john")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

attrsMap := SimplifyAttributes(attrs)

fmt.Printf("%#v\n", attrsMap)
Output:

func (*API) GetUserDirectGroups

func (api *API) GetUserDirectGroups(userName string, options ...ListingOptions) ([]*Group, error)

GetUserDirectGroups returns the groups that the user is a direct member of

Example
api, err := NewAPI("https://crowd.domain.com/crowd/", "myapp", "MySuppaPAssWOrd")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

groups, err := api.GetUserDirectGroups("john")
// with listing options
groups, err = api.GetUserDirectGroups(
	"john",
	ListingOptions{StartIndex: 100, MaxResults: 50},
)

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

if len(groups) > 0 {
	for _, group := range groups {
		fmt.Printf("%#v\n", group)
	}
}
Output:

func (*API) GetUserGroups

func (api *API) GetUserGroups(userName, groupType string, options ...ListingOptions) ([]*Group, error)

GetUserGroups returns the groups that the user is a member of

Example
api, err := NewAPI("https://crowd.domain.com/crowd/", "myapp", "MySuppaPAssWOrd")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

groups, err := api.GetUserGroups("john", GROUP_DIRECT)
// with listing options
groups, err = api.GetUserGroups(
	"john", GROUP_DIRECT,
	ListingOptions{StartIndex: 100, MaxResults: 50},
)

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

if len(groups) > 0 {
	for _, group := range groups {
		fmt.Printf("%#v\n", group)
	}
}
Output:

func (*API) GetUserNestedGroups

func (api *API) GetUserNestedGroups(userName string, options ...ListingOptions) ([]*Group, error)

GetUserNestedGroups returns the groups that the user is a nested member of

Example
api, err := NewAPI("https://crowd.domain.com/crowd/", "myapp", "MySuppaPAssWOrd")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

groups, err := api.GetUserNestedGroups("john")
// with listing options
groups, err = api.GetUserNestedGroups(
	"john",
	ListingOptions{StartIndex: 100, MaxResults: 50},
)

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

if len(groups) > 0 {
	for _, group := range groups {
		fmt.Printf("%#v\n", group)
	}
}
Output:

func (*API) Login added in v3.1.0

func (api *API) Login(userName, password string) (*User, error)

Login attempts to authenticate a user with the given username and password. It constructs a URL with the given username and sends a POST request to the usermanagement authentication API with the provided password. It returns a pointer to a User object with the user's information on successful authentication, or an error if authentication failed or an unknown error occurred.

Example
api, err := NewAPI("https://crowd.domain.com/crowd/", "myapp", "MySuppaPAssWOrd")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

currentUser, err := api.Login("john", "MySuppaPAssWOrd")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

fmt.Printf("%#v\n", currentUser)
Output:

func (*API) SearchGroups

func (api *API) SearchGroups(cql string, options ...ListingOptions) ([]*Group, error)

SearchGroups searches for groups with the specified search restriction

Example
api, err := NewAPI("https://crowd.domain.com/crowd/", "myapp", "MySuppaPAssWOrd")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

groups, err := api.SearchGroups(`name = "admin*" and active = false`)
// with listing options
groups, err = api.SearchGroups(
	`name = "admin*" and active = false`,
	ListingOptions{StartIndex: 100, MaxResults: 50},
)

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

if len(groups) > 0 {
	for _, group := range groups {
		fmt.Printf("%#v\n", group)
	}
}
Output:

func (*API) SearchUsers

func (api *API) SearchUsers(cql string, options ...ListingOptions) ([]*User, error)

SearchUsers searches for users with the specified search restriction

Example
api, err := NewAPI("https://crowd.domain.com/crowd/", "myapp", "MySuppaPAssWOrd")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

users, err := api.SearchUsers(`createdDate > 2010-12 or firstName = Jo*`)
// with listing options
users, err = api.SearchUsers(
	`createdDate > 2010-12 or firstName = Jo*`,
	ListingOptions{StartIndex: 100, MaxResults: 50},
)

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

if len(users) > 0 {
	for _, user := range users {
		fmt.Printf("%#v\n", user)
	}
}
Output:

func (*API) SetGroupAttributes

func (api *API) SetGroupAttributes(groupName string, attrs *GroupAttributes) error

SetGroupAttributes stores all the group attributes

func (*API) SetUserAgent

func (api *API) SetUserAgent(app, version string)

SetUserAgent configures user-agent string based on app name and version

Example
api, err := NewAPI("https://crowd.domain.com/crowd/", "myapp", "MySuppaPAssWOrd")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

api.SetUserAgent("MyApp", "1.2.3")
Output:

func (*API) SetUserAttributes

func (api *API) SetUserAttributes(userName string, attrs *UserAttributes) error

SetUserAttributes stores all the user attributes for an existing user

type Attribute

type Attribute struct {
	Name   string   `xml:"name,attr"`
	Values []string `xml:"values>value"`
}

Attribute contains attribute info

func (*Attribute) String

func (a *Attribute) String() string

String convert attribute to string

type Attributes

type Attributes []*Attribute

Attributes it is slice with attributes

func (Attributes) Get

func (a Attributes) Get(name string) string

Get returns merged values for attribute with given name

func (Attributes) GetList

func (a Attributes) GetList(name string) []string

GetList returns slice with values for attribute with given name

func (Attributes) Has

func (a Attributes) Has(name string) bool

Has returns true if slice contains attribute with given name

type Group

type Group struct {
	Name        string `xml:"name,attr"`
	Description string `xml:"description"`
	Type        string `xml:"type"`
	IsActive    bool   `xml:"active"`
}

Group contains info about group

type GroupAttributes

type GroupAttributes struct {
	XMLName    xml.Name     `xml:"attributes"`
	Attributes []*Attribute `xml:"attribute"`
}

GroupAttributes contains group attributes

type ListingOptions added in v3.2.0

type ListingOptions struct {
	StartIndex int
	MaxResults int
}

ListingOptions contains options for request with listing objects

func (ListingOptions) Encode added in v3.2.0

func (l ListingOptions) Encode() string

Encode encodes listing options to URL query string part

type Membership

type Membership struct {
	Group string      `xml:"group,attr"`
	Users []*UserInfo `xml:"users>user"`
}

Membership contains membership info

type User

type User struct {
	Attributes  Attributes `xml:"attributes>attribute"`
	Name        string     `xml:"name,attr"`
	FirstName   string     `xml:"first-name"`
	LastName    string     `xml:"last-name"`
	DisplayName string     `xml:"display-name"`
	Email       string     `xml:"email"`
	Key         string     `xml:"key,omitempty"`
	Password    string     `xml:"password>value,omitempty"`
	IsActive    bool       `xml:"active"`
}

User contains info about user

type UserAttributes

type UserAttributes struct {
	XMLName    xml.Name     `xml:"attributes,omitempty"`
	Attributes []*Attribute `xml:"attribute"`
}

UserAttributes contains user attributes

type UserInfo

type UserInfo struct {
	Name string `xml:"name,attr"`
}

UserInfo contains basic user info (username)

func (*UserInfo) String

func (u *UserInfo) String() string

String convert user info to string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL