github.com/kotovmak/go-admin@v1.1.1/plugins/admin/models/permission.go (about)

     1  package models
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/kotovmak/go-admin/modules/db"
     8  )
     9  
    10  // PermissionModel is permission model structure.
    11  type PermissionModel struct {
    12  	Base
    13  
    14  	Id         int64
    15  	Name       string
    16  	Slug       string
    17  	HttpMethod []string
    18  	HttpPath   []string
    19  	CreatedAt  string
    20  	UpdatedAt  string
    21  }
    22  
    23  // Permission return a default permission model.
    24  func Permission() PermissionModel {
    25  	return PermissionModel{Base: Base{TableName: "goadmin_permissions"}}
    26  }
    27  
    28  // PermissionWithId return a default permission model of given id.
    29  func PermissionWithId(id string) PermissionModel {
    30  	idInt, _ := strconv.Atoi(id)
    31  	return PermissionModel{Base: Base{TableName: "goadmin_permissions"}, Id: int64(idInt)}
    32  }
    33  
    34  func (t PermissionModel) SetConn(con db.Connection) PermissionModel {
    35  	t.Conn = con
    36  	return t
    37  }
    38  
    39  // IsEmpty check the user model is empty or not.
    40  func (t PermissionModel) IsEmpty() bool {
    41  	return t.Id == int64(0)
    42  }
    43  
    44  // IsSlugExist check the row exist with given slug and id.
    45  func (t PermissionModel) IsSlugExist(slug string, id string) bool {
    46  	if id == "" {
    47  		check, _ := t.Table(t.TableName).Where("slug", "=", slug).First()
    48  		return check != nil
    49  	}
    50  	check, _ := t.Table(t.TableName).
    51  		Where("slug", "=", slug).
    52  		Where("id", "!=", id).
    53  		First()
    54  	return check != nil
    55  }
    56  
    57  // Find return the permission model of given id.
    58  func (t PermissionModel) Find(id interface{}) PermissionModel {
    59  	item, _ := t.Table(t.TableName).Find(id)
    60  	return t.MapToModel(item)
    61  }
    62  
    63  // FindBySlug return the permission model of given slug.
    64  func (t PermissionModel) FindBySlug(slug string) PermissionModel {
    65  	item, _ := t.Table(t.TableName).Where("slug", "=", slug).First()
    66  	return t.MapToModel(item)
    67  }
    68  
    69  // FindBySlug return the permission model of given slug.
    70  func (t PermissionModel) FindByName(name string) PermissionModel {
    71  	item, _ := t.Table(t.TableName).Where("name", "=", name).First()
    72  	return t.MapToModel(item)
    73  }
    74  
    75  // MapToModel get the permission model from given map.
    76  func (t PermissionModel) MapToModel(m map[string]interface{}) PermissionModel {
    77  	t.Id = m["id"].(int64)
    78  	t.Name, _ = m["name"].(string)
    79  	t.Slug, _ = m["slug"].(string)
    80  
    81  	methods, _ := m["http_method"].(string)
    82  	if methods != "" {
    83  		t.HttpMethod = strings.Split(methods, ",")
    84  	} else {
    85  		t.HttpMethod = []string{""}
    86  	}
    87  
    88  	path, _ := m["http_path"].(string)
    89  	t.HttpPath = strings.Split(path, "\n")
    90  	t.CreatedAt, _ = m["created_at"].(string)
    91  	t.UpdatedAt, _ = m["updated_at"].(string)
    92  	return t
    93  }