github.com/ngocphuongnb/tetua@v0.0.7-alpha/app/entities/role.go (about)

     1  package entities
     2  
     3  import (
     4  	"net/url"
     5  	"time"
     6  
     7  	"github.com/ngocphuongnb/tetua/app/utils"
     8  )
     9  
    10  // Role is the model entity for the Role schema.
    11  type Role struct {
    12  	ID          int           `json:"id,omitempty"`
    13  	CreatedAt   *time.Time    `json:"created_at,omitempty"`
    14  	UpdatedAt   *time.Time    `json:"updated_at,omitempty"`
    15  	DeletedAt   *time.Time    `json:"deleted_at,omitempty"`
    16  	Name        string        `json:"name,omitempty" validate:"max=255"`
    17  	Description string        `json:"description,omitempty" validate:"max=255"`
    18  	Root        bool          `json:"root,omitempty"`
    19  	Users       []*User       `json:"users,omitempty"`
    20  	Permissions []*Permission `json:"permissions,omitempty"`
    21  }
    22  
    23  type PermType string
    24  
    25  const (
    26  	PERM_ALL  PermType = "all"
    27  	PERM_OWN  PermType = "own"
    28  	PERM_NONE PermType = "none"
    29  )
    30  
    31  func (s PermType) String() string {
    32  	switch s {
    33  	case PERM_ALL:
    34  		return "all"
    35  	case PERM_OWN:
    36  		return "own"
    37  	case PERM_NONE:
    38  		return "none"
    39  	}
    40  	return "none"
    41  }
    42  
    43  type RoleMutation struct {
    44  	Name        string             `form:"name" json:"name"`
    45  	Description string             `form:"description" json:"description"`
    46  	Root        bool               `form:"root" json:"root"`
    47  	Permissions []*PermissionValue `form:"permissions" json:"permissions"`
    48  }
    49  
    50  type PermissionValue struct {
    51  	Action string   `json:"action,omitempty" validate:"max=255"`
    52  	Value  PermType `json:"value,omitempty"`
    53  }
    54  
    55  type RolePermissions struct {
    56  	RoleID      int                `json:"role_id,omitempty"`
    57  	Permissions []*PermissionValue `json:"permissions,omitempty"`
    58  }
    59  
    60  func GetPermTypeValue(value string) PermType {
    61  	switch value {
    62  	case "all":
    63  		return PERM_ALL
    64  	case "own":
    65  		return PERM_OWN
    66  	case "none":
    67  		return PERM_NONE
    68  	default:
    69  		return PERM_NONE
    70  	}
    71  }
    72  
    73  type RoleFilter struct {
    74  	*Filter
    75  }
    76  
    77  func (p *RoleFilter) Base() string {
    78  	q := url.Values{}
    79  	if !utils.SliceContains(p.IgnoreUrlParams, "search") && p.Search != "" {
    80  		q.Add("q", p.Search)
    81  	}
    82  
    83  	if queryString := q.Encode(); queryString != "" {
    84  		return p.FilterBaseUrl() + "?" + q.Encode()
    85  	}
    86  
    87  	return p.FilterBaseUrl()
    88  }