users

package
v0.0.0-...-f3ad2b4 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2015 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AuthenticateMethod kit.Method = &methods.Method{
	Name:     "users.authenticate",
	Blocking: true,
	Handler: func(registry kit.Registry, r kit.Request, unblock func()) kit.Response {
		if r.GetUser() != nil {
			return kit.NewErrorResponse("already_authenticated", "Can't authenticate a session which is already authenticated", true)
		}

		data, ok := r.GetData().(map[string]interface{})
		if !ok {
			return kit.NewErrorResponse("invalid_data", "Invalid data: expected dict", true)
		}

		userIdentifier, _ := data["user"].(string)

		adaptor, _ := data["adaptor"].(string)
		if adaptor == "" {
			return kit.NewErrorResponse("adaptor_missing", "Expected 'adaptor' in metadata.", true)
		}

		authData, _ := data["authData"].(map[string]interface{})
		if authData == nil {
			kit.NewErrorResponse("no_or_invalid_auth_data", "Expected 'authData' dictionary in metadata.")
		}

		userService := registry.UserService()
		user, err := userService.AuthenticateUser(userIdentifier, adaptor, authData)
		if err != nil {
			return kit.NewErrorResponse(err)
		}

		session := r.GetSession()

		if err := session.SetUserId(user.GetId()); err != nil {
			return kit.NewErrorResponse(err)
		}
		if err := userService.SessionResource().Update(session, user); err != nil {
			return kit.NewErrorResponse(err)
		}

		session.SetUser(user)

		return &kit.AppResponse{
			Data: session,
		}
	},
}
View Source
var ResumeSessionMethod kit.Method = &methods.Method{
	Name:     "users.resume_session",
	Blocking: true,
	Handler: func(registry kit.Registry, r kit.Request, unblock func()) kit.Response {
		registry.Logger().Infof("data: %v", r.GetData())
		data, _ := r.GetData().(map[string]interface{})
		token, _ := data["token"].(string)

		if token == "" {
			return kit.NewErrorResponse("no_token", "Expected 'token' in data.", true)
		}

		user, session, err := registry.UserService().VerifySession(token)
		if err != nil {
			return kit.NewErrorResponse(err)
		}

		session.SetUser(user)

		curSession := r.GetSession()
		curSession.SetToken(token)
		curSession.SetValidUntil(session.GetValidUntil())
		curSession.SetUser(user)

		return &kit.AppResponse{
			Data: session,
		}
	},
}
View Source
var UnAuthenticateMethod kit.Method = &methods.Method{
	Name:     "users.unauthenticate",
	Blocking: true,
	Handler: func(registry kit.Registry, r kit.Request, unblock func()) kit.Response {
		if r.GetUser() == nil {
			return kit.NewErrorResponse("not_authenticated", "Can't un-authenticate a session which is not authenticated", true)
		}

		session := r.GetSession()
		session.SetUser(nil)
		if err := registry.UserService().SessionResource().Update(session, r.GetUser()); err != nil {
			return kit.NewErrorResponse(err)
		}

		return &kit.AppResponse{
			Data: map[string]interface{}{},
		}
	},
}

Functions

func GetUserMigrations

func GetUserMigrations(service kit.UserService) []db.Migration

Types

type IntIdUserProfile

type IntIdUserProfile struct {
	db.IntIdModel
}

func (IntIdUserProfile) Collection

func (p IntIdUserProfile) Collection() string

func (IntIdUserProfile) GetUser

func (p IntIdUserProfile) GetUser() kit.User

func (IntIdUserProfile) GetUserId

func (p IntIdUserProfile) GetUserId() interface{}

func (IntIdUserProfile) SetUser

func (p IntIdUserProfile) SetUser(user kit.User)

func (*IntIdUserProfile) SetUserId

func (p *IntIdUserProfile) SetUserId(id interface{}) error

type IntUserModel

type IntUserModel struct {
	User   *UserIntId
	UserId uint64 `db:""`
}

func (*IntUserModel) GetUser

func (m *IntUserModel) GetUser() kit.User

func (*IntUserModel) GetUserId

func (m *IntUserModel) GetUserId() interface{}

func (*IntUserModel) SetUser

func (m *IntUserModel) SetUser(u kit.User)

func (*IntUserModel) SetUserId

func (m *IntUserModel) SetUserId(id interface{}) error

type IntUserSession

type IntUserSession struct {
	IntUserModel
	Session
}

func (*IntUserSession) IsAnonymous

func (s *IntUserSession) IsAnonymous() bool

type Permission

type Permission struct {
	Name string `db:"primary-key;max:200"`
}

func (Permission) Collection

func (r Permission) Collection() string

func (Permission) GetId

func (p Permission) GetId() interface{}

func (Permission) GetName

func (r Permission) GetName() string

func (Permission) GetStrId

func (p Permission) GetStrId() string

func (*Permission) SetId

func (p *Permission) SetId(n interface{}) error

func (*Permission) SetName

func (r *Permission) SetName(n string)

func (*Permission) SetStrId

func (p *Permission) SetStrId(n string) error

type PermissionResourceHooks

type PermissionResourceHooks struct {
	resources.AdminResource
}

type Role

type Role struct {
	Name        string        `db:"primary-key;max:200"`
	Permissions []*Permission `db:"m2m"`
}

func (*Role) AddPermission

func (r *Role) AddPermission(perms ...string)

func (*Role) ClearPermissions

func (r *Role) ClearPermissions()

func (Role) Collection

func (r Role) Collection() string

func (Role) GetId

func (r Role) GetId() interface{}

func (Role) GetName

func (r Role) GetName() string

func (*Role) GetPermissions

func (r *Role) GetPermissions() []string

func (Role) GetStrId

func (r Role) GetStrId() string

func (*Role) HasPermission

func (r *Role) HasPermission(perms ...string) bool

func (*Role) RemovePermission

func (r *Role) RemovePermission(perms ...string)

func (*Role) SetId

func (r *Role) SetId(n interface{}) error

func (*Role) SetName

func (r *Role) SetName(n string)

func (*Role) SetPermissions

func (r *Role) SetPermissions(perms []string)

func (*Role) SetStrId

func (r *Role) SetStrId(n string) error

type RoleResourceHooks

type RoleResourceHooks struct {
	resources.AdminResource
}

func (RoleResourceHooks) AllowFind

func (RoleResourceHooks) AllowFind(res kit.Resource, model kit.Model, user kit.User) bool

Restrict querying to admins.

func (RoleResourceHooks) ApiAlterQuery

func (RoleResourceHooks) ApiAlterQuery(res kit.Resource, query db.Query, r kit.Request) apperror.Error

type Service

type Service struct {
	Users    kit.Resource
	Profiles kit.Resource
	Sessions kit.Resource
	Tokens   kit.Resource

	Roles       kit.Resource
	Permissions kit.Resource

	AuthAdaptors map[string]kit.AuthAdaptor
	// contains filtered or unexported fields
}

func NewService

func NewService(registry kit.Registry, backend db.Backend, profileModel kit.UserProfile) *Service

func (*Service) AddAuthAdaptor

func (h *Service) AddAuthAdaptor(a kit.AuthAdaptor)

func (*Service) AuthAdaptor

func (h *Service) AuthAdaptor(name string) kit.AuthAdaptor

func (*Service) AuthenticateUser

func (h *Service) AuthenticateUser(userIdentifier string, authAdaptorName string, data map[string]interface{}) (kit.User, apperror.Error)

func (*Service) Backend

func (s *Service) Backend() db.Backend

func (*Service) BuildToken

func (s *Service) BuildToken(typ, userId string, expiresAt time.Time) (kit.UserToken, apperror.Error)

func (*Service) ChangePassword

func (s *Service) ChangePassword(user kit.User, newPassword string) apperror.Error

func (*Service) ConfirmEmail

func (s *Service) ConfirmEmail(token string) (kit.User, apperror.Error)

func (*Service) CreateUser

func (s *Service) CreateUser(user kit.User, adaptorName string, authData map[string]interface{}) apperror.Error

func (*Service) Debug

func (s *Service) Debug() bool

func (*Service) FindUser

func (s *Service) FindUser(userIdentifier interface{}) (kit.User, apperror.Error)

FindUser tries to find a user based on either userId, user.Username or user.Email.

func (*Service) PermissionResource

func (u *Service) PermissionResource() kit.Resource

func (*Service) ProfileModel

func (h *Service) ProfileModel() kit.UserProfile

func (*Service) ProfileResource

func (h *Service) ProfileResource() kit.Resource

func (*Service) Registry

func (s *Service) Registry() kit.Registry

func (*Service) ResetPassword

func (s *Service) ResetPassword(token, newPassword string) (kit.User, apperror.Error)

func (*Service) RoleResource

func (u *Service) RoleResource() kit.Resource

func (*Service) SendConfirmationEmail

func (s *Service) SendConfirmationEmail(user kit.User) apperror.Error

func (*Service) SendPasswordResetEmail

func (s *Service) SendPasswordResetEmail(user kit.User) apperror.Error

func (*Service) SessionResource

func (h *Service) SessionResource() kit.Resource

func (*Service) SetBackend

func (s *Service) SetBackend(b db.Backend)

func (*Service) SetDebug

func (s *Service) SetDebug(x bool)

func (*Service) SetPermissionResource

func (u *Service) SetPermissionResource(x kit.Resource)

func (*Service) SetProfileResource

func (h *Service) SetProfileResource(x kit.Resource)

func (*Service) SetRegistry

func (s *Service) SetRegistry(x kit.Registry)

func (*Service) SetRoleResource

func (u *Service) SetRoleResource(x kit.Resource)

func (*Service) SetSessionResource

func (h *Service) SetSessionResource(x kit.Resource)

func (*Service) SetTokenResource

func (h *Service) SetTokenResource(x kit.Resource)

func (*Service) SetUserResource

func (h *Service) SetUserResource(x kit.Resource)

func (Service) StartSession

func (s Service) StartSession(user kit.User, sessionType string) (kit.Session, apperror.Error)

func (*Service) TokenResource

func (h *Service) TokenResource() kit.Resource

func (*Service) UserResource

func (h *Service) UserResource() kit.Resource

func (*Service) VerifySession

func (h *Service) VerifySession(token string) (kit.User, kit.Session, apperror.Error)

type Session

type Session struct {
	Token string `db:"primary-key;max:150"`
	Type  string `db:"max:10"`

	StartedAt  time.Time `db:"required"`
	ValidUntil time.Time `db:"required"`
}

func (Session) Collection

func (b Session) Collection() string

func (Session) GetId

func (s Session) GetId() interface{}

func (*Session) GetStartedAt

func (s *Session) GetStartedAt() time.Time

func (Session) GetStrId

func (s Session) GetStrId() string

func (*Session) GetToken

func (s *Session) GetToken() string

func (*Session) GetType

func (s *Session) GetType() string

func (*Session) GetValidUntil

func (s *Session) GetValidUntil() time.Time

func (*Session) SetId

func (s *Session) SetId(x interface{}) error

func (*Session) SetStartedAt

func (s *Session) SetStartedAt(x time.Time)

func (*Session) SetStrId

func (s *Session) SetStrId(x string) error

func (*Session) SetToken

func (s *Session) SetToken(x string)

func (*Session) SetType

func (s *Session) SetType(x string)

func (*Session) SetValidUntil

func (s *Session) SetValidUntil(x time.Time)

type SessionResourceHooks

type SessionResourceHooks struct {
	UpdateAllowed    bool
	ApiDeleteAllowed bool
}

func (SessionResourceHooks) ApiCreate

func (hooks SessionResourceHooks) ApiCreate(res kit.Resource, obj kit.Model, r kit.Request) kit.Response

Creating a session is equivalent to logging in.

func (SessionResourceHooks) ApiDelete

func (SessionResourceHooks) ApiFindOne

func (SessionResourceHooks) ApiFindOne(res kit.Resource, rawId string, r kit.Request) kit.Response

ApiFindOne verifies the session and returns user and profile in meta if valid.

type StrIdUserProfile

type StrIdUserProfile struct {
	db.StrIdModel
}

func (StrIdUserProfile) Collection

func (p StrIdUserProfile) Collection() string

func (StrIdUserProfile) GetUser

func (p StrIdUserProfile) GetUser() kit.User

func (StrIdUserProfile) GetUserId

func (p StrIdUserProfile) GetUserId() interface{}

func (StrIdUserProfile) SetUser

func (p StrIdUserProfile) SetUser(user kit.User)

func (*StrIdUserProfile) SetUserId

func (p *StrIdUserProfile) SetUserId(id interface{}) error

type StrUserModel

type StrUserModel struct {
	User   *UserStrId
	UserId string `db:"max:255"`
}

func (*StrUserModel) GetUser

func (m *StrUserModel) GetUser() kit.User

func (*StrUserModel) GetUserId

func (m *StrUserModel) GetUserId() interface{}

func (*StrUserModel) SetUser

func (m *StrUserModel) SetUser(u kit.User)

func (*StrUserModel) SetUserId

func (m *StrUserModel) SetUserId(id interface{}) error

type StrUserSession

type StrUserSession struct {
	StrUserModel
	Session
}

func (StrUserSession) IsAnonymous

func (s StrUserSession) IsAnonymous() bool

type Token

type Token struct {
	StrUserModel

	Token     string    `db:"primary-key"`
	Type      string    `db:"required"`
	ExpiresAt time.Time `db:"ignore-zero"`
}

func (*Token) Collection

func (t *Token) Collection() string

func (*Token) GetExpiresAt

func (t *Token) GetExpiresAt() time.Time

func (*Token) GetId

func (t *Token) GetId() interface{}

func (*Token) GetStrId

func (t *Token) GetStrId() string

func (*Token) GetToken

func (t *Token) GetToken() string

func (*Token) GetType

func (t *Token) GetType() string

func (*Token) IsValid

func (t *Token) IsValid() bool

func (*Token) SetExpiresAt

func (t *Token) SetExpiresAt(tm time.Time)

func (*Token) SetId

func (t *Token) SetId(id interface{}) error

func (*Token) SetStrId

func (t *Token) SetStrId(id string) error

func (*Token) SetToken

func (t *Token) SetToken(x string)

func (*Token) SetType

func (t *Token) SetType(x string)

type User

type User struct {
	db.TimeStampedModel

	Active bool

	Username string `db:"unique;required"`
	Email    string `db:"unique;required"`

	EmailConfirmed bool

	LastLogin time.Time `db:"ignore-zero"`

	Data string `db:"ignore-zero;max:10000"`

	Profile interface{} `db:"-"`

	Roles []*Role `db:"m2m;"`
}

func (*User) AddRole

func (u *User) AddRole(roles ...string)

func (*User) ClearRoles

func (u *User) ClearRoles()

func (User) Collection

func (u User) Collection() string

func (*User) GetCreatedAt

func (u *User) GetCreatedAt() time.Time

func (*User) GetData

func (u *User) GetData() (interface{}, apperror.Error)

func (*User) GetEmail

func (u *User) GetEmail() string

func (*User) GetLastLogin

func (u *User) GetLastLogin() time.Time

func (User) GetProfile

func (a User) GetProfile() kit.UserProfile

func (*User) GetRoles

func (u *User) GetRoles() []string

func (*User) GetUpdatedAt

func (u *User) GetUpdatedAt() time.Time

func (*User) GetUsername

func (u *User) GetUsername() string

func (*User) HasPermission

func (u *User) HasPermission(perms ...string) bool

func (*User) HasRole

func (u *User) HasRole(roles ...string) bool

func (*User) IsActive

func (u *User) IsActive() bool

func (*User) IsEmailConfirmed

func (u *User) IsEmailConfirmed() bool

func (*User) RemoveRole

func (u *User) RemoveRole(roles ...string)

func (*User) SetCreatedAt

func (u *User) SetCreatedAt(x time.Time)

func (*User) SetData

func (u *User) SetData(x interface{}) apperror.Error

func (*User) SetEmail

func (u *User) SetEmail(x string)

func (*User) SetIsActive

func (u *User) SetIsActive(x bool)

func (*User) SetIsEmailConfirmed

func (u *User) SetIsEmailConfirmed(x bool)

func (*User) SetLastLogin

func (u *User) SetLastLogin(x time.Time)

func (*User) SetRoles

func (u *User) SetRoles(roles []string)

func (*User) SetUpdatedAt

func (u *User) SetUpdatedAt(x time.Time)

func (*User) SetUsername

func (u *User) SetUsername(x string)

type UserIntId

type UserIntId struct {
	User
	db.IntIdModel
}

func (*UserIntId) SetProfile

func (u *UserIntId) SetProfile(p kit.UserProfile)

type UserResourceHooks

type UserResourceHooks struct {
}

func (UserResourceHooks) AllowDelete

func (hooks UserResourceHooks) AllowDelete(res kit.Resource, obj kit.Model, old kit.Model, user kit.User) bool

func (UserResourceHooks) AllowFind

func (hooks UserResourceHooks) AllowFind(res kit.Resource, obj kit.Model, user kit.User) bool

func (UserResourceHooks) AllowUpdate

func (hooks UserResourceHooks) AllowUpdate(res kit.Resource, obj kit.Model, old kit.Model, user kit.User) bool

func (UserResourceHooks) ApiCreate

func (hooks UserResourceHooks) ApiCreate(res kit.Resource, obj kit.Model, r kit.Request) kit.Response

func (UserResourceHooks) Methods

func (UserResourceHooks) Methods(res kit.Resource) []kit.Method

type UserStrId

type UserStrId struct {
	User
	db.StrIdModel
}

func (*UserStrId) SetProfile

func (u *UserStrId) SetProfile(p kit.UserProfile)

Directories

Path Synopsis
auth

Jump to

Keyboard shortcuts

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