Skip to content

Commit

Permalink
Adding the modification functions
Browse files Browse the repository at this point in the history
  • Loading branch information
COMTOP1 committed Feb 15, 2025
1 parent 7a51a9d commit 90eb357
Show file tree
Hide file tree
Showing 5 changed files with 432 additions and 6 deletions.
16 changes: 16 additions & 0 deletions crowd/crowd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ type (
GetCrowdApp(context.Context, CrowdApp) (CrowdApp, error)
GetCrowdApps(context.Context, CrowdAppStatus) ([]CrowdApp, error)
VerifyCrowd(context.Context, CrowdApp) (CrowdApp, error)
AddCrowdApp(context.Context, CrowdApp) (CrowdApp, error)
EditCrowdApp(context.Context, CrowdApp) (CrowdApp, error)
DeleteCrowdApp(context.Context, CrowdApp) error
}

// Store stores the dependencies
Expand Down Expand Up @@ -83,3 +86,16 @@ func (s *Store) VerifyCrowd(ctx context.Context, c CrowdApp) (CrowdApp, error) {

return c, errors.New("invalid credentials")
}

func (s *Store) AddCrowdApp(ctx context.Context, c CrowdApp) (CrowdApp, error) {
c.Password = null.StringFrom(utils.HashPass(c.Salt.String + c.Password.String))
return s.addCrowdApp(ctx, c)
}

func (s *Store) EditCrowdApp(ctx context.Context, c CrowdApp) (CrowdApp, error) {
return s.editCrowdApp(ctx, c)
}

func (s *Store) DeleteCrowdApp(ctx context.Context, c CrowdApp) error {
return s.deleteCrowdApp(ctx, c)
}
74 changes: 74 additions & 0 deletions crowd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,77 @@ func (s *Store) getCrowdApps(ctx context.Context, crowdAppStatus CrowdAppStatus)

return c, nil
}

func (s *Store) addCrowdApp(ctx context.Context, c CrowdApp) (CrowdApp, error) {
builder := utils.PSQL().Insert("web_auth.crowd_apps").
Columns("name", "username", "description", "active", "password", "salt").
Values(c.Name, c.Username, c.Description, c.Active, c.Password, c.Salt).
Suffix("RETURNING app_id")

sql, args, err := builder.ToSql()
if err != nil {
panic(fmt.Errorf("failed to build sql for addCrowdApp: %w", err))
}

stmt, err := s.db.PrepareContext(ctx, sql)
if err != nil {
return CrowdApp{}, fmt.Errorf("failed to add crowd app: %w", err)
}

defer stmt.Close()

err = stmt.QueryRow(args...).Scan(&c.AppID)
if err != nil {
return CrowdApp{}, fmt.Errorf("failed to add crowd app: %w", err)
}

return c, nil
}

func (s *Store) editCrowdApp(ctx context.Context, c CrowdApp) (CrowdApp, error) {
builder := utils.PSQL().Update("web_auth.crowd_apps").
SetMap(map[string]interface{}{
"name": c.Name,
"description": c.Description,
"active": c.Active,
}).
Where(sq.Eq{"app_id": c.AppID})

sql, args, err := builder.ToSql()
if err != nil {
panic(fmt.Errorf("failed to build sql for editCrowdApp: %w", err))
}

res, err := s.db.ExecContext(ctx, sql, args...)
if err != nil {
return CrowdApp{}, fmt.Errorf("failed to edit crowd app: %w", err)
}

rows, err := res.RowsAffected()
if err != nil {
return CrowdApp{}, fmt.Errorf("failed to edit crowd app: %w", err)
}

if rows < 1 {
return CrowdApp{}, fmt.Errorf("failed to edit crowd app: invalid rows affected: %d", rows)
}

return c, nil
}

func (s *Store) deleteCrowdApp(ctx context.Context, c CrowdApp) error {
builder := utils.PSQL().Delete("web_auth.crowd_apps").
Where(sq.Eq{"app_id": c.AppID})

sql, args, err := builder.ToSql()
if err != nil {
panic(fmt.Errorf("failed to build sql for deleteCrowdApp: %w", err))
}

_, err = s.db.ExecContext(ctx, sql, args...)
if err != nil {
return fmt.Errorf("failed to delete crowd app: %w", err)
}

return nil
}
139 changes: 139 additions & 0 deletions templates/crowdApp.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{{define "title"}}Internal: Crowd App ({{.CrowdApp.Name}}){{end}}
{{define "content"}}
<div class="column is-10" style="min-height: 88vh">
<section class="hero is-info welcome is-small">
<div class="hero-body">
<div class="container">
<h1 class="title">{{.CrowdApp.Name}}</h1>
</div>
</div>
</section>
<br>
<div class="columns box" style="height: fit-content">
<div class="column is-2">
<div class="buttons" style="display: block">
<a class="button is-warning is-outlined" onclick="editCrowdAppModal()">
<span class="mdi mdi-pencil"></span>&ensp;Edit
</a>
<a class="button is-danger is-outlined" onclick="deleteCrowdAppModal()">
<span class="mdi mdi-account-multiple-minus"></span>&ensp;Delete
</a>
</div>
</div>
<div class="column">
{{with .CrowdApp}}
<p>
App ID: {{.AppID}}<br>
Name: {{.Name}}<br>
Username: {{.Username}}<br>
Description: {{.Description.String}}<br>
Active: {{if .Active}}active{{else}}inactive{{end}}<br>
</p>
{{end}}
</div>
</div>
</div>
{{template "modals" .}}
{{end}}

{{define "modals"}}
{{with .CrowdApp}}
<div id="editCrowdAppModal" class="modal">
<div class="modal-background"></div>
<div class="modal-content">
<div class="box">
<article class="media">
<div class="media-content">
<div class="content">
<p class="title">Are you sure you want to edit this crowd app?</p>
<p><strong>This action can be undone by changing them back but be careful</strong><br>
Modifications may affect other applications and the usability, proceed with caution<br>
Use the fields below to modify the details</p>
<form action="/internal/crowdapp/{{.AppID}}/edit" method="post">
<div class="field">
<label class="label" for="name">Name</label>
<div class="control">
<input
id="name"
class="input"
type="text"
name="name"
placeholder="Name"
value="{{.Name}}"
/>
</div>
</div>
<div class="field">
<label class="label" for="description">Description</label>
<div class="control">
<textarea
id="description"
class="input"
name="description"
placeholder="Description"
>{{.Description.String}}</textarea>
</div>
</div>
<div class="field">
<label class="label" for="active">Active</label>
<div class="control">
<input
id="active"
class="checkbox"
type="checkbox"
name="active"
{{if .Active}}checked{{end}}
/>
</div>
</div>
<button class="button is-danger"><span class="mdi mdi-pencil"></span>&ensp;Edit
crowd app
</button>
</form>
</div>
</div>
</article>
</div>
</div>
<button class="modal-close is-large" aria-label="close"></button>
</div>
<div id="deleteCrowdAppModal" class="modal">
<div class="modal-background"></div>
<div class="modal-content">
<div class="box">
<article class="media">
<div class="media-content">
<div class="content">
<p class="title">Are you sure you want to delete this crowd app?</p>
<p>Be careful! Applications both public and internal could use this and will have to
set back up manually.</p>
<form action="/internal/crowdapp/{{.AppID}}/delete" method="post">
<button class="button is-danger">Delete crowd app</button>
</form>
</div>
</div>
</article>
</div>
</div>
<button class="modal-close is-large" aria-label="close"></button>
</div>
{{end}}
<script>
document.querySelectorAll(
".modal-background, .modal-close,.modal-card-head .delete, .modal-card-foot .button"
).forEach(($el) => {
const $modal = $el.closest(".modal");
$el.addEventListener("click", () => {
$modal.classList.remove("is-active");
});
});

function editCrowdAppModal() {
document.getElementById("editCrowdAppModal").classList.add("is-active");
}

function deleteCrowdAppModal() {
document.getElementById("deleteCrowdAppModal").classList.add("is-active");
}
</script>
{{end}}
1 change: 1 addition & 0 deletions templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const (
OfficershipTeamsTemplate Template = "officershipTeams.tmpl"
OfficershipTeamTemplate Template = "officershipTeam.tmpl"
CrowdAppsTemplate Template = "crowdApps.tmpl"
CrowdAppTemplate Template = "crowdApp.tmpl"
)

type TemplateType int
Expand Down
Loading

0 comments on commit 90eb357

Please sign in to comment.