user

package
v0.0.0-...-36cd75f Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2019 License: GPL-3.0 Imports: 22 Imported by: 0

Documentation

Overview

Uploading avatar from file.

Sets UseGravatar to false.

Index

Constants

View Source
const ROLES_ALL = 1 // all Roles in all Trees
View Source
const ROLES_GLOBAL = 0 // only global Roles (default)
View Source
const ROLES_TREE = 1 // all user Roles in a specific Tree

Variables

View Source
var (
	IdentityKey      = "user"
	SigningAlgorithm = "HS256"

	Timeout = time.Hour

	// This field allows clients to refresh their token until MaxRefresh has passed.
	// Note that clients can refresh their token in the last moment of MaxRefresh.
	// This means that the maximum validity timespan for a token is TokenTime + MaxRefresh.
	// Optional, defaults to 0 meaning not refreshable.
	MaxRefresh = time.Hour

	ErrMissingLoginValues  = errors.New("no login values")
	ErrInvalidHash         = errors.New("the encoded hash is not in the correct format")
	ErrIncompatibleVersion = errors.New("incompatible version of argon2")

	// ErrExpiredToken indicates JWT token has expired. Can't refresh.
	ErrExpiredToken            = errors.New("token is expired")
	ErrEmptyCookieToken        = errors.New("empty token")
	ErrInvalidSigningAlgorithm = errors.New("invalid alg")
	ErrFailedAuthentication    = errors.New("failed auth")
)

Functions

func AvatarPOST

func AvatarPOST(c *gin.Context)

POST

func ErrUnauthorized

func ErrUnauthorized(c *gin.Context, msg string)

func GetClaimsFromJWT

func GetClaimsFromJWT(c *gin.Context) (jwt.MapClaims, error)

func ParseToken

func ParseToken(c *gin.Context) (*jwt.Token, error)

ParseToken parse jwt token from gin context

func Routes

func Routes(r *gin.Engine)

func SetLogger

func SetLogger(logger *zap.Logger)

func UploadAvatarEndpoint

func UploadAvatarEndpoint(c *gin.Context)

PUT

func UserPatchEndpoint

func UserPatchEndpoint(c *gin.Context)

UserPatchEndpoint modifies fields of a User model.

func UserWebsocketEndpoint

func UserWebsocketEndpoint(c *gin.Context)

Types

type GetRolesOptions

type GetRolesOptions struct {
	Roles int
	Tree  *tree.Tree
}

type Image

type Image struct {
	// Id          int64  `json:"id,string"`
	// Id uuid.UUID `sql:",type:uuid" json:"-"`
	Id []byte // SHA-1 of the image (20 bytes)

	W int `sql:",notnull"`
	H int `sql:",notnull"`

	//ObjectHash []byte // SHA-1 takes 20 bytes (in binary form)
	OriginalId []byte // SHA-1 of the original image

}

Image model was initially created for avatars. So given an Image we need to quickly get different sizes of that image.

If an original image was cropped current record will have W, H and ObjectHash of a cropped image and OriginalId pointing to an original one that was cropped.

type PlainPassword

type PlainPassword = string

type PostgresStorage

type PostgresStorage struct {
	Conn *pg.DB
}

func (*PostgresStorage) AddImage

func (s *PostgresStorage) AddImage(img *Image) (*Image, error)

func (*PostgresStorage) Authenticate

func (s *PostgresStorage) Authenticate(email string, password string) (*User, error)

This is the main function which checks user/password. Returns 2 values: (User object, error). One of them is nil.

func (*PostgresStorage) CreateNewUser

func (s *PostgresStorage) CreateNewUser(email string) (*User, PlainPassword, error)

func (*PostgresStorage) GetRoles

func (s *PostgresStorage) GetRoles(u *User, options *GetRolesOptions) ([]*Role, error)

select ur.user_id from roles role join user_roles ur on role.id=ur.role_id where ur.tree_id is NULL; func (s *PostgresStorage) GetUserGlobalRoles(u *User) ([]*Role, error)

func (*PostgresStorage) GetUserByEmail

func (s *PostgresStorage) GetUserByEmail(email string) (*User, error)

func (*PostgresStorage) GetUserById

func (s *PostgresStorage) GetUserById(id int64) (*User, error)

func (*PostgresStorage) Save

func (s *PostgresStorage) Save(user *User) error

func (*PostgresStorage) SaveFields

func (s *PostgresStorage) SaveFields(user *User, fields ...string) error

fields must be specified as they are in a database (case-sensitive)

type Role

type Role struct {
	Id int64 `json:"Id,string"`
	// Tree int64 `json:"Tree,string"`
	Name string

	Superuser bool

	// Tree *tree.Tree `pg:"fk:tree_id"`
	TreeId int64
	Tree   tree.Tree

	Users []*User `pg:"many2many:user_roles" json:"-"` // many to many relation
}

Trees can have a set of roles

func (*Role) IsSuperuser

func (role *Role) IsSuperuser() bool

type User

type User struct {
	Id        int64  `json:"id,string"`
	FirstName string `json:"firstname"`
	LastName  string `json:"lastname"`
	Email     string `json:"email"`
	Password  string `json:"-"`

	UseGravatar bool `sql:",notnull"`
	// AvatarId    uuid.UUID `sql:",type:uuid" json:"-"`
	AvatarId []byte // `sql:"-"` // json:"-"
	Avatar   *Image //`sql:"-"`

	Data map[string]string

	Lng   string  `json:"lng"`
	Roles []*Role `pg:"many2many:user_roles,joinFK:role_id" json:"-"`
}

func FromGinContext

func FromGinContext(c *gin.Context) *User

FromGinContext creates a pseudo-user if a user is logged in. This user has only one parameter set - it's ID.

func GetUserByID62

func GetUserByID62(id62 string) (*User, error)

func (*User) CanRemoveTree

func (u *User) CanRemoveTree(t *tree.Tree) bool

func (*User) GetGlobalRoles

func (u *User) GetGlobalRoles() []*Role

func (*User) GetPermissionsForTree

func (u *User) GetPermissionsForTree(t *tree.Tree) []string

func (*User) GetRoles

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

func (*User) GetRolesInTree

func (u *User) GetRolesInTree(t *tree.Tree) []*Role

func (*User) HasPermission

func (u *User) HasPermission(permission string, args ...interface{}) (bool, error)

func (*User) ID62

func (user *User) ID62() string

type UserStorage

type UserStorage interface {
	GetUserById(id int64) (*User, error)
	GetUserByEmail(email string) (*User, error)
	Authenticate(email string, password string) (*User, error)
	CreateNewUser(email string) (*User, PlainPassword, error)

	GetRoles(u *User, options *GetRolesOptions) ([]*Role, error)

	Save(u *User) error
	SaveFields(u *User, fields ...string) error

	AddImage(img *Image) (*Image, error)
}
var (
	Storage UserStorage
)

Jump to

Keyboard shortcuts

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