admin page updates
This commit is contained in:
@@ -69,6 +69,34 @@ func (a *AuditLogFilter) Result(result string) *AuditLogFilter {
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *AuditLogFilter) UserIDs(ids []int) *AuditLogFilter {
|
||||
if len(ids) > 0 {
|
||||
a.In("al.user_id", ids)
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *AuditLogFilter) Actions(actions []string) *AuditLogFilter {
|
||||
if len(actions) > 0 {
|
||||
a.In("al.action", actions)
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *AuditLogFilter) ResourceTypes(resourceTypes []string) *AuditLogFilter {
|
||||
if len(resourceTypes) > 0 {
|
||||
a.In("al.resource_type", resourceTypes)
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *AuditLogFilter) Results(results []string) *AuditLogFilter {
|
||||
if len(results) > 0 {
|
||||
a.In("al.result", results)
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *AuditLogFilter) DateRange(start, end int64) *AuditLogFilter {
|
||||
if start > 0 {
|
||||
a.GreaterEqualThan("al.created_at", start)
|
||||
@@ -83,7 +111,7 @@ func (a *AuditLogFilter) DateRange(start, end int64) *AuditLogFilter {
|
||||
func GetAuditLogs(ctx context.Context, tx bun.Tx, pageOpts *PageOpts, filters *AuditLogFilter) (*List[AuditLog], error) {
|
||||
defaultPageOpts := &PageOpts{
|
||||
Page: 1,
|
||||
PerPage: 15,
|
||||
PerPage: 10,
|
||||
Order: bun.OrderDesc,
|
||||
OrderBy: "created_at",
|
||||
}
|
||||
@@ -119,7 +147,7 @@ func GetAuditLogByID(ctx context.Context, tx bun.Tx, id int) (*AuditLog, error)
|
||||
if id <= 0 {
|
||||
return nil, errors.New("id must be positive")
|
||||
}
|
||||
return GetByID[AuditLog](tx, id).Relation("User").Get(ctx)
|
||||
return GetByField[AuditLog](tx, "al.id", id).Relation("User").Get(ctx)
|
||||
}
|
||||
|
||||
// GetUniqueActions retrieves a list of all unique actions in the audit log
|
||||
|
||||
@@ -33,6 +33,7 @@ const (
|
||||
LessEqual Comparator = "<="
|
||||
Greater Comparator = ">"
|
||||
GreaterEqual Comparator = ">="
|
||||
In Comparator = "IN"
|
||||
)
|
||||
|
||||
type ListFilter struct {
|
||||
@@ -63,6 +64,10 @@ func (f *ListFilter) GreaterEqualThan(field string, value any) {
|
||||
f.filters = append(f.filters, Filter{field, value, GreaterEqual})
|
||||
}
|
||||
|
||||
func (f *ListFilter) In(field string, values any) {
|
||||
f.filters = append(f.filters, Filter{field, values, In})
|
||||
}
|
||||
|
||||
func GetList[T any](tx bun.Tx) *listgetter[T] {
|
||||
l := &listgetter[T]{
|
||||
items: new([]*T),
|
||||
@@ -89,7 +94,11 @@ func (l *listgetter[T]) Relation(name string, apply ...func(*bun.SelectQuery) *b
|
||||
|
||||
func (l *listgetter[T]) Filter(filters ...Filter) *listgetter[T] {
|
||||
for _, filter := range filters {
|
||||
l.q = l.q.Where("? ? ?", bun.Ident(filter.Field), bun.Safe(filter.Comparator), filter.Value)
|
||||
if filter.Comparator == In {
|
||||
l.q = l.q.Where("? IN (?)", bun.Ident(filter.Field), bun.In(filter.Value))
|
||||
} else {
|
||||
l.q = l.q.Where("? ? ?", bun.Ident(filter.Field), bun.Safe(filter.Comparator), filter.Value)
|
||||
}
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
@@ -61,6 +61,17 @@ func ListAllRoles(ctx context.Context, tx bun.Tx) ([]*Role, error) {
|
||||
return GetList[Role](tx).GetAll(ctx)
|
||||
}
|
||||
|
||||
// GetRoles returns a paginated list of roles
|
||||
func GetRoles(ctx context.Context, tx bun.Tx, pageOpts *PageOpts) (*List[Role], error) {
|
||||
defaults := &PageOpts{
|
||||
Page: 1,
|
||||
PerPage: 25,
|
||||
Order: bun.OrderAsc,
|
||||
OrderBy: "display_name",
|
||||
}
|
||||
return GetList[Role](tx).GetPaged(ctx, pageOpts, defaults)
|
||||
}
|
||||
|
||||
// CreateRole creates a new role
|
||||
func CreateRole(ctx context.Context, tx bun.Tx, role *Role) error {
|
||||
if role == nil {
|
||||
|
||||
Reference in New Issue
Block a user