server/dao/postgres/RightDAO.go

147 lines
3.2 KiB
Go

package postgres
import (
"git.tek.govt.hu/dowerx/chat/server/model"
"git.tek.govt.hu/dowerx/chat/server/util"
)
type RightDAOPG struct {
pgDAO
}
func (d RightDAOPG) Grant(role model.Role, channel model.Channel, rights model.RightEnum) *util.ChatError {
if role.ID != 0 {
role.Name = ""
}
if channel.ID != 0 {
channel.Name = ""
}
_, err := d.db.Exec(
`insert into "right" ("role_id", "channel_id", "rights") values (
(select "id" from "role" where "id" = $1 or "name" = $2),
(select "id" from "channel" where "id" = $3 or "name" = $4),
$5
)`, role.ID, role.Name, channel.ID, channel.Name, rights)
return util.MakeError(err, util.DATABASE_QUERY_FAULT)
}
func (d RightDAOPG) Revoke(role model.Role, channel model.Channel) *util.ChatError {
if role.ID != 0 {
role.Name = ""
}
if channel.ID != 0 {
channel.Name = ""
}
_, err := d.db.Exec(
`delete from "right"
where
"role_id" = (select "id" from "role" where "id" = $1 or "name" = $2)
and
"channel_id" = (select "id" from "channel" where "id" = $3 or "name" = $4)`,
role.ID, role.Name, channel.ID, channel.Name)
return util.MakeError(err, util.DATABASE_QUERY_FAULT)
}
func (d RightDAOPG) Update(role model.Role, channel model.Channel, rights model.RightEnum) *util.ChatError {
if role.ID != 0 {
role.Name = ""
}
if channel.ID != 0 {
channel.Name = ""
}
_, err := d.db.Exec(
`update "right" set "rights" = $5
where
"role_id" = (select "id" from "role" where "id" = $1 or "name" = $2)
and
"channel_id" = (select "id" from "channel" where "id" = $3 or "name" = $4)`,
role.ID, role.Name, channel.ID, channel.Name, rights)
return util.MakeError(err, util.DATABASE_QUERY_FAULT)
}
func (d RightDAOPG) ListRoleRights(role model.Role) (map[int]model.RightEnum, *util.ChatError) {
if role.ID != 0 {
role.Name = ""
}
rows, err := d.db.NamedQuery(
`select
"channel_id", "rights"
from "rights"
where "role_id" = (select "id" from "role" where "id" = :id or "name" = :name)`,
&role)
if err != nil {
return nil, util.MakeError(err, util.DATABASE_QUERY_FAULT)
}
result := make(map[int]model.RightEnum)
var channel_id int
var rights model.RightEnum
for rows.Next() {
err = rows.Scan(&channel_id, &rights)
if err != nil {
return nil, util.MakeError(err, util.DATABASE_QUERY_FAULT)
}
result[channel_id] = rights
}
return result, nil
}
func (d RightDAOPG) ListUserRigths(user model.User) (map[int]model.RightEnum, *util.ChatError) {
if user.ID != 0 {
user.Username = ""
}
rows, err := d.db.NamedQuery(
`select
"channel_id", "rights"
from "user_rigths_per_channel"
where "user_id" = (select "id" from "user" where "id" = :id or "username" = :username)`,
&user)
if err != nil {
return nil, util.MakeError(err, util.DATABASE_QUERY_FAULT)
}
result := make(map[int]model.RightEnum)
var channel_id int
var rights model.RightEnum
for rows.Next() {
err = rows.Scan(&channel_id, &rights)
if err != nil {
return nil, util.MakeError(err, util.DATABASE_QUERY_FAULT)
}
result[channel_id] = rights
}
return result, nil
}
func MakeRightDAO() (RightDAOPG, *util.ChatError) {
dao := RightDAOPG{}
conn, err := getDatabase()
if err != nil {
return dao, err
}
dao.db = conn
return dao, nil
}