Documentation ¶
Overview ¶
Package users contains the domain concept definitions needed to support Magistrala users service functionality.
This package defines the core domain concepts and types necessary to handle users in the context of a Magistrala users service. It abstracts the underlying complexities of user management and provides a structured approach to working with users.
Index ¶
Constants ¶
const ( Disabled = "disabled" Enabled = "enabled" Deleted = "deleted" All = "all" Unknown = "unknown" )
String representation of the possible status values.
const (
Admin = "admin"
)
String representation of the possible role values.
Variables ¶
var ( // ErrEnableClient indicates error in enabling client. ErrEnableClient = errors.New("failed to enable client") // ErrDisableClient indicates error in disabling client. ErrDisableClient = errors.New("failed to disable client") )
Functions ¶
func NewDeleteHandler ¶ added in v0.15.0
func NewDeleteHandler(ctx context.Context, users Repository, policyService policies.Service, domainsClient magistrala.DomainsServiceClient, defCheckInterval, deleteAfter time.Duration, logger *slog.Logger)
Types ¶
type Credentials ¶ added in v0.15.0
type Emailer ¶
type Emailer interface { // SendPasswordReset sends an email to the user with a link to reset the password. SendPasswordReset(To []string, host, user, token string) error }
Emailer wrapper around the email.
type Hasher ¶
type Hasher interface { // Hash generates the hashed string from plain-text. Hash(string) (string, error) // Compare compares plain-text version to the hashed one. An error should // indicate failed comparison. Compare(string, string) error }
Hasher specifies an API for generating hashes of an arbitrary textual content.
type MembersPage ¶ added in v0.15.0
MembersPage contains page related metadata as well as list of members that belong to this page.
type Metadata ¶ added in v0.15.0
type Metadata map[string]interface{}
Metadata represents arbitrary JSON.
type Page ¶ added in v0.15.0
type Page struct { Total uint64 `json:"total"` Offset uint64 `json:"offset"` Limit uint64 `json:"limit"` Id string `json:"id,omitempty"` Order string `json:"order,omitempty"` Dir string `json:"dir,omitempty"` Metadata Metadata `json:"metadata,omitempty"` Domain string `json:"domain,omitempty"` Tag string `json:"tag,omitempty"` Permission string `json:"permission,omitempty"` Status Status `json:"status,omitempty"` IDs []string `json:"ids,omitempty"` Role Role `json:"-"` ListPerms bool `json:"-"` Username string `json:"username,omitempty"` FirstName string `json:"first_name,omitempty"` LastName string `json:"last_name,omitempty"` Email string `json:"email,omitempty"` }
Page contains page metadata that helps navigation.
type Repository ¶ added in v0.15.0
type Repository interface { // RetrieveByID retrieves user by their unique ID. RetrieveByID(ctx context.Context, id string) (User, error) // RetrieveAll retrieves all users. RetrieveAll(ctx context.Context, pm Page) (UsersPage, error) // RetrieveByEmail retrieves user by its unique credentials. RetrieveByEmail(ctx context.Context, email string) (User, error) // RetrieveByUsername retrieves user by its unique credentials. RetrieveByUsername(ctx context.Context, username string) (User, error) // Update updates the user name and metadata. Update(ctx context.Context, user User) (User, error) // UpdateUsername updates the User's names. UpdateUsername(ctx context.Context, user User) (User, error) // UpdateSecret updates secret for user with given email. UpdateSecret(ctx context.Context, user User) (User, error) // ChangeStatus changes user status to enabled or disabled ChangeStatus(ctx context.Context, user User) (User, error) // Delete deletes user with given id Delete(ctx context.Context, id string) error // Searchusers retrieves users based on search criteria. SearchUsers(ctx context.Context, pm Page) (UsersPage, error) // RetrieveAllByIDs retrieves for given user IDs . RetrieveAllByIDs(ctx context.Context, pm Page) (UsersPage, error) CheckSuperAdmin(ctx context.Context, adminID string) error // Save persists the user account. A non-nil error is returned to indicate // operation failure. Save(ctx context.Context, user User) (User, error) }
type Role ¶ added in v0.15.0
type Role uint8
Role represents User role.
const ( UserRole Role = iota AdminRole // AllRole is used for querying purposes to list users irrespective // of their role - both admin and user. It is never stored in the // database as the actual user role and should always be the largest // value in this enumeration. AllRole )
Possible User role values.
func (Role) MarshalJSON ¶ added in v0.15.0
func (*Role) UnmarshalJSON ¶ added in v0.15.0
type Service ¶
type Service interface { // Register creates new user. In case of the failed registration, a // non-nil error value is returned. Register(ctx context.Context, session authn.Session, user User, selfRegister bool) (User, error) // View retrieves user info for a given user ID and an authorized token. View(ctx context.Context, session authn.Session, id string) (User, error) // ViewProfile retrieves user info for a given token. ViewProfile(ctx context.Context, session authn.Session) (User, error) // ListUsers retrieves users list for a valid auth token. ListUsers(ctx context.Context, session authn.Session, pm Page) (UsersPage, error) // ListMembers retrieves everything that is assigned to a group/thing identified by objectID. ListMembers(ctx context.Context, session authn.Session, objectKind, objectID string, pm Page) (MembersPage, error) // SearchUsers searches for users with provided filters for a valid auth token. SearchUsers(ctx context.Context, pm Page) (UsersPage, error) // Update updates the user's name and metadata. Update(ctx context.Context, session authn.Session, user User) (User, error) // UpdateTags updates the user's tags. UpdateTags(ctx context.Context, session authn.Session, user User) (User, error) // UpdateEmail updates the user's email. UpdateEmail(ctx context.Context, session authn.Session, id, email string) (User, error) // UpdateUsername updates the user's username. UpdateUsername(ctx context.Context, session authn.Session, id, username string) (User, error) // UpdateProfilePicture updates the user's profile picture. UpdateProfilePicture(ctx context.Context, session authn.Session, user User) (User, error) // GenerateResetToken email where mail will be sent. // host is used for generating reset link. GenerateResetToken(ctx context.Context, email, host string) error // UpdateSecret updates the user's secret. UpdateSecret(ctx context.Context, session authn.Session, oldSecret, newSecret string) (User, error) // ResetSecret change users secret in reset flow. // token can be authentication token or secret reset token. ResetSecret(ctx context.Context, session authn.Session, secret string) error // SendPasswordReset sends reset password link to email. SendPasswordReset(ctx context.Context, host, email, user, token string) error // UpdateRole updates the user's Role. UpdateRole(ctx context.Context, session authn.Session, user User) (User, error) // Enable logically enables the user identified with the provided ID. Enable(ctx context.Context, session authn.Session, id string) (User, error) // Disable logically disables the user identified with the provided ID. Disable(ctx context.Context, session authn.Session, id string) (User, error) // Delete deletes user with given ID. Delete(ctx context.Context, session authn.Session, id string) error // Identify returns the user id from the given token. Identify(ctx context.Context, session authn.Session) (string, error) // IssueToken issues a new access and refresh token when provided with either a username or email. IssueToken(ctx context.Context, identity, secret string) (*magistrala.Token, error) // RefreshToken refreshes expired access tokens. // After an access token expires, the refresh token is used to get // a new pair of access and refresh tokens. RefreshToken(ctx context.Context, session authn.Session, refreshToken string) (*magistrala.Token, error) // OAuthCallback handles the callback from any supported OAuth provider. // It processes the OAuth tokens and either signs in or signs up the user based on the provided state. OAuthCallback(ctx context.Context, user User) (User, error) // OAuthAddUserPolicy adds a policy to the user for an OAuth request. OAuthAddUserPolicy(ctx context.Context, user User) error }
Service specifies an API that must be fullfiled by the domain service implementation, and all of its decorators (e.g. logging & metrics).
func NewService ¶
func NewService(token magistrala.TokenServiceClient, urepo Repository, policyService policies.Service, emailer Emailer, hasher Hasher, idp magistrala.IDProvider) Service
NewService returns a new Users service implementation.
type Status ¶ added in v0.15.0
type Status uint8
Status represents User status.
const ( // EnabledStatus represents enabled User. EnabledStatus Status = iota // DisabledStatus represents disabled User. DisabledStatus // DeletedStatus represents a user that will be deleted. DeletedStatus // AllStatus is used for querying purposes to list users irrespective // of their status - both enabled and disabled. It is never stored in the // database as the actual User status and should always be the largest // value in this enumeration. AllStatus )
Possible User status values.
func (Status) MarshalJSON ¶ added in v0.15.0
Custom Marshaller for Uesr/Groups.
func (*Status) UnmarshalJSON ¶ added in v0.15.0
Custom Unmarshaler for User/Groups.
type User ¶ added in v0.15.0
type User struct { ID string `json:"id"` FirstName string `json:"first_name,omitempty"` LastName string `json:"last_name,omitempty"` Tags []string `json:"tags,omitempty"` Metadata Metadata `json:"metadata,omitempty"` Status Status `json:"status"` // 0 for enabled, 1 for disabled Role Role `json:"role"` // 0 for normal user, 1 for admin ProfilePicture string `json:"profile_picture,omitempty"` // profile picture URL Credentials Credentials `json:"credentials,omitempty"` Permissions []string `json:"permissions,omitempty"` Email string `json:"email,omitempty"` CreatedAt time.Time `json:"created_at,omitempty"` UpdatedAt time.Time `json:"updated_at,omitempty"` UpdatedBy string `json:"updated_by,omitempty"` }
type UserRepository ¶ added in v0.15.0
UserRepository struct implements the Repository interface.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package api contains API-related concerns: endpoint definitions, middlewares and all resource representations.
|
Package api contains API-related concerns: endpoint definitions, middlewares and all resource representations. |
Package emailer contains the domain concept definitions needed to support Magistrala users email service functionality.
|
Package emailer contains the domain concept definitions needed to support Magistrala users email service functionality. |
Package events provides the domain concept definitions needed to support Magistrala users service functionality.
|
Package events provides the domain concept definitions needed to support Magistrala users service functionality. |
Package hasher contains the domain concept definitions needed to support Magistrala users password hasher sub-service functionality.
|
Package hasher contains the domain concept definitions needed to support Magistrala users password hasher sub-service functionality. |
Package middleware provides middleware for Magistrala Users service.
|
Package middleware provides middleware for Magistrala Users service. |
Package mocks contains mocks for testing purposes.
|
Package mocks contains mocks for testing purposes. |
Package postgres contains the database implementation of users repository layer.
|
Package postgres contains the database implementation of users repository layer. |
Package tracing provides tracing instrumentation for Magistrala Users service.
|
Package tracing provides tracing instrumentation for Magistrala Users service. |