github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/db/permission.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package db
    20  
    21  import (
    22  	"context"
    23  
    24  	"github.com/e154/smart-home/common/apperr"
    25  	"github.com/pkg/errors"
    26  	"gorm.io/gorm"
    27  )
    28  
    29  // Permissions ...
    30  type Permissions struct {
    31  	Db *gorm.DB
    32  }
    33  
    34  // Permission ...
    35  type Permission struct {
    36  	Id          int64 `gorm:"primary_key"`
    37  	Role        *Role `gorm:"foreignkey:RoleName"`
    38  	RoleName    string
    39  	PackageName string
    40  	LevelName   string
    41  }
    42  
    43  // TableName ...
    44  func (m *Permission) TableName() string {
    45  	return "permissions"
    46  }
    47  
    48  // Add ...
    49  func (n Permissions) Add(ctx context.Context, permission *Permission) (id int64, err error) {
    50  	if err = n.Db.WithContext(ctx).Create(&permission).Error; err != nil {
    51  		err = errors.Wrap(apperr.ErrPermissionAdd, err.Error())
    52  		return
    53  	}
    54  	id = permission.Id
    55  	return
    56  }
    57  
    58  // Delete ...
    59  func (n Permissions) Delete(ctx context.Context, roleName, packageName string, levelName []string) (err error) {
    60  
    61  	err = n.Db.WithContext(ctx).
    62  		Delete(&Permission{}, "role_name = ? and package_name = ? and level_name in (?)", roleName, packageName, levelName).
    63  		Error
    64  	if err != nil {
    65  		err = errors.Wrap(apperr.ErrPermissionDelete, err.Error())
    66  	}
    67  
    68  	return
    69  }
    70  
    71  // GetAllPermissions ...
    72  func (n Permissions) GetAllPermissions(ctx context.Context, name string) (permissions []*Permission, err error) {
    73  
    74  	permissions = make([]*Permission, 0)
    75  	err = n.Db.WithContext(ctx).Raw(`
    76  WITH RECURSIVE r AS (
    77      SELECT name, description, parent, created_at, updated_at, 1 AS level
    78      FROM roles
    79      WHERE name = ?
    80  
    81          UNION
    82  
    83          SELECT roles.name, roles.description, roles.parent, roles.created_at, roles.updated_at, r.level + 1 AS level
    84          FROM roles
    85                 JOIN r
    86                   ON roles.name = r.parent
    87      )
    88  
    89  SELECT DISTINCT p.*
    90  FROM r
    91  left join permissions p on p.role_name = r.name
    92  where p notnull
    93  order by p.id;
    94  `, name).
    95  		Scan(&permissions).
    96  		Error
    97  	if err != nil {
    98  		err = errors.Wrap(apperr.ErrPermissionGet, err.Error())
    99  	}
   100  	return
   101  }