github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/initial/local_migrations/m_update_permissions.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 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 local_migrations
    20  
    21  import (
    22  	"context"
    23  	"github.com/e154/smart-home/system/orm"
    24  
    25  	"github.com/e154/smart-home/adaptors"
    26  	m "github.com/e154/smart-home/models"
    27  	"github.com/e154/smart-home/system/access_list"
    28  	. "github.com/e154/smart-home/system/initial/assertions"
    29  )
    30  
    31  type MigrationUpdatePermissions struct {
    32  	adaptors   *adaptors.Adaptors
    33  	accessList access_list.AccessListService
    34  	orm        *orm.Orm
    35  }
    36  
    37  func NewMigrationUpdatePermissions(adaptors *adaptors.Adaptors,
    38  	accessList access_list.AccessListService,
    39  	orm *orm.Orm) *MigrationUpdatePermissions {
    40  	return &MigrationUpdatePermissions{
    41  		adaptors:   adaptors,
    42  		accessList: accessList,
    43  		orm:        orm,
    44  	}
    45  }
    46  
    47  func (r *MigrationUpdatePermissions) Up(ctx context.Context, adaptors *adaptors.Adaptors) (err error) {
    48  	if adaptors != nil {
    49  		r.adaptors = adaptors
    50  	}
    51  
    52  	if _, err = r.truncatePermissions(ctx); err != nil {
    53  		return
    54  	}
    55  
    56  	if _, err = r.updateDemo(ctx); err != nil {
    57  		return
    58  	}
    59  
    60  	if _, err = r.updateUser(ctx); err != nil {
    61  		return
    62  	}
    63  
    64  	return
    65  }
    66  
    67  func (r *MigrationUpdatePermissions) truncatePermissions(ctx context.Context) (adminRole *m.Role, err error) {
    68  	_, err = r.orm.DB().Exec("truncate table permissions")
    69  	return
    70  }
    71  
    72  func (r *MigrationUpdatePermissions) updateUser(ctx context.Context) (userRole *m.Role, err error) {
    73  
    74  	if userRole, err = r.adaptors.Role.GetByName(ctx, "user"); err == nil {
    75  		for pack, item := range *r.accessList.List(ctx) {
    76  			for levelName, right := range item {
    77  				if right.Method == "put" || right.Method == "post" || right.Method == "delete" {
    78  					permission := &m.Permission{
    79  						RoleName:    userRole.Name,
    80  						PackageName: pack,
    81  						LevelName:   levelName,
    82  					}
    83  
    84  					_, err = r.adaptors.Permission.Add(ctx, permission)
    85  					So(err, ShouldBeNil)
    86  				}
    87  			}
    88  		}
    89  	}
    90  
    91  	return
    92  }
    93  
    94  func (r *MigrationUpdatePermissions) updateDemo(ctx context.Context) (demoRole *m.Role, err error) {
    95  
    96  	if demoRole, err = r.adaptors.Role.GetByName(ctx, "demo"); err == nil {
    97  		for pack, item := range *r.accessList.List(ctx) {
    98  			for levelName, right := range item {
    99  				if right.Method == "get" {
   100  					permission := &m.Permission{
   101  						RoleName:    demoRole.Name,
   102  						PackageName: pack,
   103  						LevelName:   levelName,
   104  					}
   105  
   106  					_, err = r.adaptors.Permission.Add(ctx, permission)
   107  					So(err, ShouldBeNil)
   108  				}
   109  			}
   110  		}
   111  	}
   112  
   113  	return
   114  }