Skip to content

Commit

Permalink
feat: list devices based on permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Jan 21, 2024
1 parent b8c5154 commit 2436614
Show file tree
Hide file tree
Showing 18 changed files with 939 additions and 233 deletions.
3 changes: 1 addition & 2 deletions internal/migrations/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ CREATE TABLE dahua_permissions (
user_id INTEGER,
group_id INTEGER,
device_id INTEGER NOT NULL,
read BOOLEAN NOT NULL,
write BOOLEAN NOT NULL,
level INTEGER NOT NULL,
UNIQUE (user_id, device_id),
UNIQUE (group_id, device_id),
FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE CASCADE ON DELETE CASCADE,
Expand Down
25 changes: 25 additions & 0 deletions internal/migrations/sql/20240121020400_initial.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- +goose Up
-- disable the enforcement of foreign-keys constraints
PRAGMA foreign_keys = off;
-- create "new_dahua_permissions" table
CREATE TABLE `new_dahua_permissions` (`user_id` integer NULL, `group_id` integer NULL, `device_id` integer NOT NULL, `level` integer NOT NULL, CONSTRAINT `0` FOREIGN KEY (`device_id`) REFERENCES `dahua_devices` (`id`) ON UPDATE CASCADE ON DELETE CASCADE, CONSTRAINT `1` FOREIGN KEY (`group_id`) REFERENCES `groups` (`id`) ON UPDATE CASCADE ON DELETE CASCADE, CONSTRAINT `2` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE CASCADE ON DELETE CASCADE);
-- copy rows from old table "dahua_permissions" to new temporary table "new_dahua_permissions"
INSERT INTO `new_dahua_permissions` (`user_id`, `group_id`, `device_id`) SELECT `user_id`, `group_id`, `device_id` FROM `dahua_permissions`;
-- drop "dahua_permissions" table after copying rows
DROP TABLE `dahua_permissions`;
-- rename temporary table "new_dahua_permissions" to "dahua_permissions"
ALTER TABLE `new_dahua_permissions` RENAME TO `dahua_permissions`;
-- create index "dahua_permissions_user_id_device_id" to table: "dahua_permissions"
CREATE UNIQUE INDEX `dahua_permissions_user_id_device_id` ON `dahua_permissions` (`user_id`, `device_id`);
-- create index "dahua_permissions_group_id_device_id" to table: "dahua_permissions"
CREATE UNIQUE INDEX `dahua_permissions_group_id_device_id` ON `dahua_permissions` (`group_id`, `device_id`);
-- enable back the enforcement of foreign-keys constraints
PRAGMA foreign_keys = on;

-- +goose Down
-- reverse: create index "dahua_permissions_group_id_device_id" to table: "dahua_permissions"
DROP INDEX `dahua_permissions_group_id_device_id`;
-- reverse: create index "dahua_permissions_user_id_device_id" to table: "dahua_permissions"
DROP INDEX `dahua_permissions_user_id_device_id`;
-- reverse: create "new_dahua_permissions" table
DROP TABLE `new_dahua_permissions`;
3 changes: 2 additions & 1 deletion internal/migrations/sql/atlas.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
h1:o8JBZ3wyYZ6BcnXbnExh/xAzDZodh/60NdLuOgOAeUY=
h1:OALKVgKef9vRTB38+EIZnMsM75/+feypay54rrqeLOA=
20240114082916_initial.sql h1:6T2vRM1rfcsNzTh9YR0sXB8U+T3GZ6PiJcZc23WvXl8=
20240117044510_initial.sql h1:91cIrQzdHSR9DIvMzRnuBlbTptlf877EieEYXT3wRaU=
20240117054837_initial.sql h1:Xw8Z7y27y9fCrv5VnGjb5W1XwtHClBgiGr92AlRL9pM=
20240117055231_initial.sql h1:/CD25FfvoH5yUaDdzIMnZ0HOUV1LJm9jxpKoP+mKWSU=
20240118025315_initial.sql h1:0NT8HEKbPi2uMxuMv0SYyyhg821qZ+SRzGoH//8ytrA=
20240120004552_initial.sql h1:PG61nxPoo4MSEgZCy7Vojc+txW8GOS4tv57rSiswzwE=
20240121020400_initial.sql h1:PVapHcXZ7bgNOp0W6ZkT/VMJnjk2NUUVWOtt6Ktt7ms=
22 changes: 22 additions & 0 deletions internal/models/dahua.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (lhs DahuaConn) EQ(rhs DahuaConn) bool {
type DahuaDeviceConn struct {
DahuaDevice
DahuaConn
Level DahuaPermissionLevel
}

type DahuaFeature int
Expand Down Expand Up @@ -227,3 +228,24 @@ type DahuaStorageDestination struct {
Password string
RemoteDirectory string
}

type DahuaPermissionLevel int

const (
DahuaPermissionLevelUser DahuaPermissionLevel = iota
DahuaPermissionLevelOperator
DahuaPermissionLevelAdmin
)

func (l DahuaPermissionLevel) String() string {
switch l {
case DahuaPermissionLevelUser:
return "user"
case DahuaPermissionLevelOperator:
return "operator"
case DahuaPermissionLevelAdmin:
return "admin"
default:
return "unknown"
}
}
31 changes: 31 additions & 0 deletions internal/repo/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (c ListDahuaDeviceByIDsRow) Convert() models.DahuaDeviceConn {
Feature: c.Feature,
Seed: int(c.Seed),
},
Level: models.DahuaPermissionLevelAdmin,
}
}

Expand All @@ -51,6 +52,7 @@ func (c GetDahuaDeviceRow) Convert() models.DahuaDeviceConn {
Feature: c.Feature,
Seed: int(c.Seed),
},
Level: models.DahuaPermissionLevelAdmin,
}
}

Expand All @@ -76,6 +78,7 @@ func (c GetDahuaDeviceByIPRow) Convert() models.DahuaDeviceConn {
Feature: c.Feature,
Seed: int(c.Seed),
},
Level: models.DahuaPermissionLevelAdmin,
}
}

Expand All @@ -101,6 +104,7 @@ func (c ListDahuaDeviceRow) Convert() models.DahuaDeviceConn {
Feature: c.Feature,
Seed: int(c.Seed),
},
Level: models.DahuaPermissionLevelAdmin,
}
}

Expand All @@ -126,6 +130,33 @@ func (c ListDahuaDeviceByFeatureRow) Convert() models.DahuaDeviceConn {
Feature: c.Feature,
Seed: int(c.Seed),
},
Level: models.DahuaPermissionLevelAdmin,
}
}

func (c ListDahuaDeviceForUserRow) Convert() models.DahuaDeviceConn {
return models.DahuaDeviceConn{
DahuaDevice: models.DahuaDevice{
ID: c.ID,
Url: c.Url.URL,
Username: c.Username,
Password: c.Password,
Location: c.Location.Location,
Name: c.Name,
CreatedAt: c.CreatedAt.Time,
UpdatedAt: c.UpdatedAt.Time,
Feature: c.Feature,
},
DahuaConn: models.DahuaConn{
ID: c.ID,
Url: c.Url.URL,
Username: c.Username,
Password: c.Password,
Location: c.Location.Location,
Feature: c.Feature,
Seed: int(c.Seed),
},
Level: c.Level,
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/repo/sqlc_db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions internal/repo/sqlc_models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 30 additions & 2 deletions internal/repo/sqlc_query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ WHERE
-- name: ListGroupForUser :many
SELECT
g.*
FROM groups AS g LEFT JOIN group_users AS gu ON gu.group_id = g.id
WHERE gu.user_id = ?;
FROM
groups AS g
LEFT JOIN group_users AS gu ON gu.group_id = g.id
WHERE
gu.user_id = ?;

-- name: createDahuaDevice :one
INSERT INTO
Expand Down Expand Up @@ -205,6 +208,31 @@ FROM
WHERE
id IN (sqlc.slice ('ids'));

-- name: ListDahuaDeviceForUser :many
SELECT
d.*,
coalesce(s.seed, d.id) AS seed,
coalesce(p.level, 2)
FROM
dahua_devices as d
LEFT JOIN dahua_seeds AS s ON s.device_id = d.id
LEFT JOIN dahua_permissions AS p ON p.device_id = d.id
WHERE
true = sqlc.arg ('admin')
OR p.user_id = sqlc.arg ('user_id')
OR p.group_id IN (
SELECT
group_id
FROM
group_users
WHERE
user_id = sqlc.arg ('user_id')
)
GROUP BY
d.id
ORDER BY
p.level DESC;

-- name: listDahuaDeviceByFeature :many
SELECT
dahua_devices.*,
Expand Down
91 changes: 88 additions & 3 deletions internal/repo/sqlc_query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions internal/rpcserver/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package rpcserver

import (
"context"
"fmt"
"time"

"github.com/ItsNotGoodName/ipcmanview/internal/core"
"github.com/ItsNotGoodName/ipcmanview/internal/repo"
"github.com/ItsNotGoodName/ipcmanview/internal/types"
"github.com/ItsNotGoodName/ipcmanview/rpc"
Expand All @@ -14,6 +16,26 @@ type Page struct {
DB repo.DB
}

func (p *Page) Home(ctx context.Context, req *rpc.PageHomeReq) (*rpc.PageHomeResp, error) {
authSession := useAuthSession(ctx)

dbDevices, err := p.DB.ListDahuaDeviceForUser(ctx, repo.ListDahuaDeviceForUserParams{
Admin: authSession.Admin,
UserID: core.Int64ToNullInt64(authSession.UserID),
})
if err != nil {
return nil, NewError(err).Internal()
}

for _, lddfur := range dbDevices {
fmt.Println(lddfur.ID, lddfur.Level)
}

return &rpc.PageHomeResp{
DeviceCount: int64(len(dbDevices)),
}, nil
}

func (p *Page) Profile(ctx context.Context, req *rpc.PageProfileReq) (*rpc.PageProfileResp, error) {
authSession := useAuthSession(ctx)

Expand Down
8 changes: 8 additions & 0 deletions internal/web/src/pages/Home.data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { cache } from "@solidjs/router";
import { useClient } from "~/providers/client";

export const getHome = cache(() => useClient().page.home({}).then((req) => req.response), "getHome")

export function loadHome() {
void getHome()
}
Loading

0 comments on commit 2436614

Please sign in to comment.