Skip to content
This repository was archived by the owner on Jul 12, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion cmd/server/assets/admin/realms/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,22 @@
</a>
</div>

<div class="card-body">
<form method="GET" action="/admin/realms" id="search-form">
<div class="input-group">
<span class="input-group-prepend">
<div class="input-group-text bg-transparent">
<span class="oi oi-magnifying-glass" aria-hidden="true"></span>
</div>
</span>
<input type="search" name="q" id="search" value="{{.query}}" placeholder="Search"
autocomplete="off" class="form-control" />
</div>
</form>
</div>

{{if $realms}}
<table class="table table-bordered table-striped table-fixed table-inner-border-only mb-0">
<table class="table table-bordered table-striped table-fixed table-inner-border-only border-top mb-0">
<thead>
<tr>
<th scope="col" width="50" class="text-center">ID</th>
Expand Down Expand Up @@ -54,6 +68,8 @@
</p>
{{end}}
</div>

{{template "shared/pagination" .}}
</main>
</body>
</html>
Expand Down
13 changes: 12 additions & 1 deletion pkg/controller/admin/realms.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,23 @@ import (

"github.com/google/exposure-notifications-verification-server/pkg/controller"
"github.com/google/exposure-notifications-verification-server/pkg/database"
"github.com/google/exposure-notifications-verification-server/pkg/pagination"
"github.com/gorilla/mux"
)

func (c *Controller) HandleRealmsIndex() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

realms, err := c.db.GetRealms()
pageParams, err := pagination.FromRequest(r)
if err != nil {
controller.InternalError(w, r, c.h, err)
return
}

q := r.FormValue(QueryKeySearch)

realms, paginator, err := c.db.ListRealms(pageParams, database.WithRealmSearch(q))
if err != nil {
controller.InternalError(w, r, c.h, err)
return
Expand All @@ -36,6 +45,8 @@ func (c *Controller) HandleRealmsIndex() http.Handler {
m := controller.TemplateMapFromContext(ctx)
m.Title("Realms - System Admin")
m["realms"] = realms
m["query"] = q
m["paginator"] = paginator
c.h.RenderHTML(w, "admin/realms/index", m)
})
}
Expand Down
26 changes: 19 additions & 7 deletions pkg/database/realm.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,15 +848,27 @@ func (db *Database) FindRealm(id interface{}) (*Realm, error) {
return &realm, nil
}

func (db *Database) GetRealms() ([]*Realm, error) {
// ListRealms lists all available realms in the system.
func (db *Database) ListRealms(p *pagination.PageParams, scopes ...Scope) ([]*Realm, *pagination.Paginator, error) {
var realms []*Realm
if err := db.db.
Order("name ASC").
Find(&realms).
Error; err != nil {
return nil, err
query := db.db.
Model(&Realm{}).
Scopes(scopes...).
Order("name ASC")

if p == nil {
p = new(pagination.PageParams)
}

paginator, err := Paginate(query, &realms, p.Page, p.Limit)
if err != nil {
if IsNotFound(err) {
return realms, nil, nil
}
return nil, nil, err
}
return realms, nil

return realms, paginator, nil
}

func (r *Realm) AuditID() string {
Expand Down
13 changes: 13 additions & 0 deletions pkg/database/scopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,16 @@ func WithAuthorizedAppSearch(q string) Scope {
return db
}
}

// WithRealmSearch returns a scope that adds querying for realms by name. It's
// only applicable to functions that query Realm.
func WithRealmSearch(q string) Scope {
return func(db *gorm.DB) *gorm.DB {
q = project.TrimSpace(q)
if q != "" {
q = `%` + q + `%`
return db.Where("realms.name ILIKE ?", q)
}
return db
}
}