Compare commits
2
Commits
a4b0abf556
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
00f13377af | ||
|
|
63c95b581a |
No files matched your search
@@ -9,13 +9,14 @@ type RolePermissionModel struct {
|
||||
}
|
||||
|
||||
type RolePageModel struct {
|
||||
ID string `db:"id"`
|
||||
Name string `db:"name"`
|
||||
Icon sql.NullString `db:"icon"`
|
||||
Url sql.NullString `db:"url"`
|
||||
Level int `db:"level"`
|
||||
Sort int `db:"sort"`
|
||||
ParentId sql.NullString `db:"parent"`
|
||||
ID string `db:"id"`
|
||||
Name string `db:"name"`
|
||||
Icon sql.NullString `db:"icon"`
|
||||
Url sql.NullString `db:"url"`
|
||||
Level int `db:"level"`
|
||||
Sort int `db:"sort"`
|
||||
ParentId sql.NullString `db:"parent"`
|
||||
Permission RolePagePermissionModel
|
||||
}
|
||||
|
||||
type RoleUserModel struct {
|
||||
@@ -36,4 +37,8 @@ type RolePagePermissionModel struct {
|
||||
ID string `db:"id"`
|
||||
IdPermission string `db:"id_permission"`
|
||||
IdPage string `db:"id_page"`
|
||||
Create bool `db:"create"`
|
||||
Read bool `db:"read"`
|
||||
Update bool `db:"update"`
|
||||
Delete bool `db:"delete"`
|
||||
}
|
||||
@@ -38,6 +38,7 @@ type accessRepo struct {
|
||||
func NewRepository(dbService database.Service) IAccessRepository {
|
||||
queryBuilder := queryUtils.NewQueryBuilder(queryUtils.DBTypePostgreSQL).SetAllowedColumns([]string{
|
||||
"id", "name", "is_active", "keycloak_id", "id_user", "id_permission", "email", "created_at", "id_page", "sort", "level",
|
||||
"\"create\"", "read", "update", "delete",
|
||||
}).SetAllowedTables([]string{TBL_USER})
|
||||
|
||||
queryBuilder.SetSecurityOptions(false, 100)
|
||||
@@ -170,12 +171,14 @@ func (r accessRepo) GetAvailablePageByKeycloakId(c context.Context, keycloakId s
|
||||
}
|
||||
|
||||
var pageIds []string
|
||||
mapPagePermission := make(map[string]RolePagePermissionModel)
|
||||
fetchPagePermission, err := r.getPageIdsByPermissionIds(c, dbconn, permissionIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, pg := range fetchPagePermission {
|
||||
pageIds = append(pageIds, pg.IdPage)
|
||||
mapPagePermission[pg.IdPage] = pg
|
||||
}
|
||||
|
||||
result, err = r.getPageByIds(c, dbconn, pageIds)
|
||||
@@ -183,6 +186,12 @@ func (r accessRepo) GetAvailablePageByKeycloakId(c context.Context, keycloakId s
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// map result page with permission
|
||||
for i := range len(result) {
|
||||
log.Println(result[i].ID)
|
||||
result[i].Permission = mapPagePermission[result[i].ID]
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -364,12 +373,19 @@ func (r accessRepo) DetailRolePermission(c context.Context, permission_id string
|
||||
|
||||
// fetch eligible page for role
|
||||
eligiblePagesMap := make(map[string]bool)
|
||||
eligiblePagesAccessMap := make(map[string]AvailableMenuAccessResponse)
|
||||
eligiblePages, err := r.getPageIdsByPermissionIds(c, dbconn, []string{permission_id})
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
for _, p := range eligiblePages {
|
||||
eligiblePagesMap[p.IdPage] = true
|
||||
eligiblePagesAccessMap[p.IdPage] = AvailableMenuAccessResponse{
|
||||
Create: p.Create,
|
||||
Update: p.Update,
|
||||
Read: p.Read,
|
||||
Delete: p.Delete,
|
||||
}
|
||||
}
|
||||
|
||||
for _, p := range allPages {
|
||||
@@ -384,6 +400,7 @@ func (r accessRepo) DetailRolePermission(c context.Context, permission_id string
|
||||
IsActive: isActive,
|
||||
Sort: p.Sort,
|
||||
ParentId: &p.ParentId.String,
|
||||
Access: eligiblePagesAccessMap[p.ID],
|
||||
})
|
||||
}
|
||||
|
||||
@@ -425,7 +442,7 @@ func (r accessRepo) UpdateRolePermission(c context.Context, req UpdateRolePageRe
|
||||
}
|
||||
|
||||
// inserting new permission
|
||||
err = r.insertPagePermission(c, tx, req.ID, pageIds)
|
||||
err = r.insertPagePermission(c, tx, req, pageIds)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
@@ -779,6 +796,10 @@ func (r accessRepo) getPageIdsByPermissionIds(c context.Context, db *sqlx.DB, pe
|
||||
{Expression: "id"},
|
||||
{Expression: "id_permission"},
|
||||
{Expression: "id_page"},
|
||||
{Expression: "create"},
|
||||
{Expression: "read"},
|
||||
{Expression: "update"},
|
||||
{Expression: "delete"},
|
||||
},
|
||||
Filters: []queryUtils.FilterGroup{
|
||||
{
|
||||
@@ -819,20 +840,35 @@ func (r accessRepo) deletePageIdsByPermissionId(c context.Context, tx *sql.Tx, p
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r accessRepo) insertPagePermission(c context.Context, tx *sql.Tx, permissionId string, pageIds []string) error {
|
||||
func (r accessRepo) insertPagePermission(c context.Context, tx *sql.Tx, req UpdateRolePageRequest, pageIds []string) error {
|
||||
if len(pageIds) > 0 {
|
||||
var valueUserPermission [][]interface{}
|
||||
mapPageUserAccess := make(map[string]AvailableMenuAccessResponse)
|
||||
|
||||
permissionId := req.ID
|
||||
|
||||
for _, page := range req.AccessPage {
|
||||
mapPageUserAccess[page.ID] = page.Access
|
||||
}
|
||||
|
||||
for _, pageId := range pageIds {
|
||||
id := uuid.New().String()
|
||||
itemValues := []interface{}{id, permissionId, pageId}
|
||||
itemValues := []interface{}{
|
||||
id,
|
||||
permissionId,
|
||||
pageId,
|
||||
mapPageUserAccess[pageId].Create,
|
||||
mapPageUserAccess[pageId].Read,
|
||||
mapPageUserAccess[pageId].Update,
|
||||
mapPageUserAccess[pageId].Delete,
|
||||
}
|
||||
|
||||
valueUserPermission = append(valueUserPermission, itemValues)
|
||||
}
|
||||
|
||||
insertPagePermissionQuery := queryUtils.InsertBulkData{
|
||||
Columns: []string{
|
||||
"id", "id_permission", "id_page",
|
||||
"id", "id_permission", "id_page", "\"create\"", "read", "update", "delete",
|
||||
},
|
||||
Values: valueUserPermission,
|
||||
}
|
||||
|
||||
@@ -2,11 +2,19 @@ package access
|
||||
|
||||
import "antrian-operasi/internal/shared"
|
||||
|
||||
type AvailableMenuAccessResponse struct {
|
||||
Create bool `json:"create"`
|
||||
Read bool `json:"read"`
|
||||
Update bool `json:"update"`
|
||||
Delete bool `json:"delete"`
|
||||
}
|
||||
|
||||
type AvailableMenuChildResponse struct {
|
||||
Title string `json:"title"`
|
||||
Icon string `json:"icon"`
|
||||
To string `json:"to"`
|
||||
Children []*AvailableMenuChildResponse `json:"children"`
|
||||
Access AvailableMenuAccessResponse `json:"access"`
|
||||
}
|
||||
|
||||
type AvailableMenuResponse struct {
|
||||
@@ -24,9 +32,16 @@ func MapMenuModelToResponse(pages []RolePageModel) []AvailableMenuResponse {
|
||||
// First pass: create all menu items
|
||||
for _, p := range pages {
|
||||
pageMap[p.ID] = &AvailableMenuChildResponse{
|
||||
|
||||
Title: p.Name,
|
||||
Icon: p.Icon.String,
|
||||
To: p.Url.String,
|
||||
Access: AvailableMenuAccessResponse{
|
||||
Create: p.Permission.Create,
|
||||
Read: p.Permission.Read,
|
||||
Update: p.Permission.Update,
|
||||
Delete: p.Permission.Delete,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,11 +90,12 @@ type ListRolePermissionPaginateResponse struct {
|
||||
}
|
||||
|
||||
type AccessPage struct {
|
||||
ID string `json:"id" binding:"required"`
|
||||
Page string `json:"page"`
|
||||
IsActive bool `json:"is_active" binding:"required"`
|
||||
Sort int `json:"sort"`
|
||||
ParentId *string `json:"parent_id" binding:"uuid"`
|
||||
ID string `json:"id" binding:"required"`
|
||||
Page string `json:"page"`
|
||||
IsActive bool `json:"is_active" binding:"required"`
|
||||
Sort int `json:"sort"`
|
||||
ParentId *string `json:"parent_id" binding:"uuid"`
|
||||
Access AvailableMenuAccessResponse `json:"access"`
|
||||
}
|
||||
|
||||
type DetailRolePageResponse struct {
|
||||
|
||||
Reference in New Issue
Block a user