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

     1  package models
     2  
     3  import (
     4  	"database/sql"
     5  	"strconv"
     6  	"time"
     7  
     8  	"github.com/kotovmak/go-admin/modules/db"
     9  	"github.com/kotovmak/go-admin/modules/db/dialect"
    10  )
    11  
    12  // RoleModel is role model structure.
    13  type RoleModel struct {
    14  	Base
    15  
    16  	Id        int64
    17  	Name      string
    18  	Slug      string
    19  	CreatedAt string
    20  	UpdatedAt string
    21  }
    22  
    23  // Role return a default role model.
    24  func Role() RoleModel {
    25  	return RoleModel{Base: Base{TableName: "goadmin_roles"}}
    26  }
    27  
    28  // RoleWithId return a default role model of given id.
    29  func RoleWithId(id string) RoleModel {
    30  	idInt, _ := strconv.Atoi(id)
    31  	return RoleModel{Base: Base{TableName: "goadmin_roles"}, Id: int64(idInt)}
    32  }
    33  
    34  func (t RoleModel) SetConn(con db.Connection) RoleModel {
    35  	t.Conn = con
    36  	return t
    37  }
    38  
    39  func (t RoleModel) WithTx(tx *sql.Tx) RoleModel {
    40  	t.Tx = tx
    41  	return t
    42  }
    43  
    44  // Find return a default role model of given id.
    45  func (t RoleModel) Find(id interface{}) RoleModel {
    46  	item, _ := t.Table(t.TableName).Find(id)
    47  	return t.MapToModel(item)
    48  }
    49  
    50  // IsSlugExist check the row exist with given slug and id.
    51  func (t RoleModel) IsSlugExist(slug string, id string) bool {
    52  	if id == "" {
    53  		check, _ := t.Table(t.TableName).Where("slug", "=", slug).First()
    54  		return check != nil
    55  	}
    56  	check, _ := t.Table(t.TableName).
    57  		Where("slug", "=", slug).
    58  		Where("id", "!=", id).
    59  		First()
    60  	return check != nil
    61  }
    62  
    63  // New create a role model.
    64  func (t RoleModel) New(name, slug string) (RoleModel, error) {
    65  
    66  	id, err := t.WithTx(t.Tx).Table(t.TableName).Insert(dialect.H{
    67  		"name": name,
    68  		"slug": slug,
    69  	})
    70  
    71  	t.Id = id
    72  	t.Name = name
    73  	t.Slug = slug
    74  
    75  	return t, err
    76  }
    77  
    78  // Update update the role model.
    79  func (t RoleModel) Update(name, slug string) (int64, error) {
    80  
    81  	return t.WithTx(t.Tx).Table(t.TableName).
    82  		Where("id", "=", t.Id).
    83  		Update(dialect.H{
    84  			"name":       name,
    85  			"slug":       slug,
    86  			"updated_at": time.Now().Format("2006-01-02 15:04:05"),
    87  		})
    88  }
    89  
    90  // CheckPermission check the permission of role.
    91  func (t RoleModel) CheckPermission(permissionId string) bool {
    92  	checkPermission, _ := t.Table("goadmin_role_permissions").
    93  		Where("permission_id", "=", permissionId).
    94  		Where("role_id", "=", t.Id).
    95  		First()
    96  	return checkPermission != nil
    97  }
    98  
    99  // DeletePermissions delete all the permissions of role.
   100  func (t RoleModel) DeletePermissions() error {
   101  	return t.WithTx(t.Tx).Table("goadmin_role_permissions").
   102  		Where("role_id", "=", t.Id).
   103  		Delete()
   104  }
   105  
   106  // AddPermission add the permissions to the role.
   107  func (t RoleModel) AddPermission(permissionId string) (int64, error) {
   108  	if permissionId != "" {
   109  		if !t.CheckPermission(permissionId) {
   110  			return t.WithTx(t.Tx).Table("goadmin_role_permissions").
   111  				Insert(dialect.H{
   112  					"permission_id": permissionId,
   113  					"role_id":       t.Id,
   114  				})
   115  		}
   116  	}
   117  	return 0, nil
   118  }
   119  
   120  // MapToModel get the role model from given map.
   121  func (t RoleModel) MapToModel(m map[string]interface{}) RoleModel {
   122  	t.Id = m["id"].(int64)
   123  	t.Name, _ = m["name"].(string)
   124  	t.Slug, _ = m["slug"].(string)
   125  	t.CreatedAt, _ = m["created_at"].(string)
   126  	t.UpdatedAt, _ = m["updated_at"].(string)
   127  	return t
   128  }