2019-07-21 05:41:30 +00:00
|
|
|
package permissions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Database sql wrapper for operations with browser objects.
|
|
|
|
type Database struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes database.
|
|
|
|
func (db Database) Close() error {
|
|
|
|
return db.db.Close()
|
|
|
|
}
|
|
|
|
|
2019-07-25 05:35:09 +00:00
|
|
|
func NewDB(db *sql.DB) *Database {
|
|
|
|
return &Database{db: db}
|
2019-07-21 05:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DappPermissions struct {
|
|
|
|
Name string `json:"dapp"`
|
|
|
|
Permissions []string `json:"permissions,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) AddPermissions(perms DappPermissions) (err error) {
|
2020-05-14 10:51:32 +00:00
|
|
|
tx, err := db.db.Begin()
|
2019-07-21 05:41:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
2020-05-14 10:51:32 +00:00
|
|
|
|
|
|
|
dInsert, err := tx.Prepare("INSERT OR REPLACE INTO dapps(name) VALUES(?)")
|
2019-07-21 05:41:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
_, err = dInsert.Exec(perms.Name)
|
|
|
|
dInsert.Close()
|
2019-07-21 05:41:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
|
2019-07-21 05:41:30 +00:00
|
|
|
if len(perms.Permissions) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
pInsert, err := tx.Prepare("INSERT INTO permissions(dapp_name, permission) VALUES(?, ?)")
|
2019-07-21 05:41:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
defer pInsert.Close()
|
2019-07-21 05:41:30 +00:00
|
|
|
for _, perm := range perms.Permissions {
|
2020-05-14 10:51:32 +00:00
|
|
|
_, err = pInsert.Exec(perms.Name, perm)
|
2019-07-21 05:41:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetPermissions() (rst []DappPermissions, err error) {
|
2020-05-14 10:51:32 +00:00
|
|
|
tx, err := db.db.Begin()
|
2019-07-21 05:41:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
2020-05-14 10:51:32 +00:00
|
|
|
|
2019-07-21 05:41:30 +00:00
|
|
|
// FULL and RIGHT joins are not supported
|
2020-05-14 10:51:32 +00:00
|
|
|
dRows, err := tx.Query("SELECT name FROM dapps")
|
2019-07-21 05:41:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
defer dRows.Close()
|
2019-07-21 05:41:30 +00:00
|
|
|
dapps := map[string]*DappPermissions{}
|
2020-05-14 10:51:32 +00:00
|
|
|
for dRows.Next() {
|
2019-07-21 05:41:30 +00:00
|
|
|
perms := DappPermissions{}
|
2020-05-14 10:51:32 +00:00
|
|
|
err = dRows.Scan(&perms.Name)
|
2019-07-21 05:41:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dapps[perms.Name] = &perms
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
|
|
|
|
pRows, err := tx.Query("SELECT dapp_name, permission from permissions")
|
2019-07-21 05:41:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
defer pRows.Close()
|
2019-07-21 05:41:30 +00:00
|
|
|
var (
|
|
|
|
name string
|
|
|
|
permission string
|
|
|
|
)
|
2020-05-14 10:51:32 +00:00
|
|
|
for pRows.Next() {
|
|
|
|
err = pRows.Scan(&name, &permission)
|
2019-07-21 05:41:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dapps[name].Permissions = append(dapps[name].Permissions, permission)
|
|
|
|
}
|
|
|
|
rst = make([]DappPermissions, 0, len(dapps))
|
|
|
|
for key := range dapps {
|
|
|
|
rst = append(rst, *dapps[key])
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
|
2019-07-21 05:41:30 +00:00
|
|
|
return rst, nil
|
|
|
|
}
|
|
|
|
|
2021-12-21 15:44:37 +00:00
|
|
|
func (db *Database) GetPermissionsByDappName(dappName string) (rst *DappPermissions, err error) {
|
|
|
|
tx, err := db.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
|
|
|
|
rst = &DappPermissions{
|
|
|
|
Name: dappName,
|
|
|
|
}
|
|
|
|
|
|
|
|
pRows, err := tx.Query("SELECT permission from permissions WHERE dapp_name = ?", dappName)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer pRows.Close()
|
|
|
|
|
|
|
|
var permission string
|
|
|
|
for pRows.Next() {
|
|
|
|
err = pRows.Scan(&permission)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rst.Permissions = append(rst.Permissions, permission)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rst, nil
|
|
|
|
}
|
|
|
|
|
2019-07-21 05:41:30 +00:00
|
|
|
func (db *Database) DeletePermission(name string) error {
|
|
|
|
_, err := db.db.Exec("DELETE FROM dapps WHERE name = ?", name)
|
|
|
|
return err
|
|
|
|
}
|
2021-12-21 15:44:37 +00:00
|
|
|
|
|
|
|
func (db *Database) DeleteDappPermission(dappName, permission string) error {
|
|
|
|
_, err := db.db.Exec("DELETE FROM permissions WHERE dapp_name = ? AND permission = ?", dappName, permission)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) HasPermission(dappName string, permission string) (bool, error) {
|
|
|
|
var count uint64
|
|
|
|
err := db.db.QueryRow(`SELECT COUNT(1) FROM permissions WHERE dapp_name = ? AND permission = ?`, dappName, permission).Scan(&count)
|
|
|
|
return count > 0, err
|
|
|
|
}
|