-
-
Notifications
You must be signed in to change notification settings - Fork 837
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fea: allow adding repositories and auth methods
- Loading branch information
Showing
12 changed files
with
596 additions
and
66 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
extensions/package-manager/js/src/admin/components/AuthMethodModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import Modal, { IInternalModalAttrs } from 'flarum/common/components/Modal'; | ||
import Mithril from 'mithril'; | ||
import app from 'flarum/admin/app'; | ||
import Select from 'flarum/common/components/Select'; | ||
import Stream from 'flarum/common/utils/Stream'; | ||
import Button from 'flarum/common/components/Button'; | ||
import extractText from 'flarum/common/utils/extractText'; | ||
|
||
export interface IAuthMethodModalAttrs extends IInternalModalAttrs { | ||
onsubmit: (type: string, host: string, token: string) => void; | ||
type?: string; | ||
host?: string; | ||
token?: string; | ||
} | ||
|
||
export default class AuthMethodModal<CustomAttrs extends IAuthMethodModalAttrs = IAuthMethodModalAttrs> extends Modal<CustomAttrs> { | ||
protected type!: Stream<string>; | ||
protected host!: Stream<string>; | ||
protected token!: Stream<string>; | ||
|
||
oninit(vnode: Mithril.Vnode<CustomAttrs, this>) { | ||
super.oninit(vnode); | ||
|
||
this.type = Stream(this.attrs.type || 'bearer'); | ||
this.host = Stream(this.attrs.host || ''); | ||
this.token = Stream(this.attrs.token || ''); | ||
} | ||
|
||
className(): string { | ||
return 'AuthMethodModal Modal--small'; | ||
} | ||
|
||
title(): Mithril.Children { | ||
return app.translator.trans('flarum-package-manager.admin.auth_config.add_button_label'); | ||
} | ||
|
||
content(): Mithril.Children { | ||
const types = { | ||
'github-oauth': app.translator.trans('flarum-package-manager.admin.auth_config.types.github-oauth'), | ||
'gitlab-oauth': app.translator.trans('flarum-package-manager.admin.auth_config.types.gitlab-oauth'), | ||
'gitlab-token': app.translator.trans('flarum-package-manager.admin.auth_config.types.gitlab-token'), | ||
bearer: app.translator.trans('flarum-package-manager.admin.auth_config.types.bearer'), | ||
}; | ||
|
||
return ( | ||
<div className="Modal-body"> | ||
<div className="Form-group"> | ||
<label>{app.translator.trans('flarum-package-manager.admin.auth_config.add_modal.type_label')}</label> | ||
<Select options={types} value={this.type()} onchange={this.type} /> | ||
</div> | ||
<div className="Form-group"> | ||
<label>{app.translator.trans('flarum-package-manager.admin.auth_config.add_modal.host_label')}</label> | ||
<input | ||
className="FormControl" | ||
bidi={this.host} | ||
placeholder={app.translator.trans('flarum-package-manager.admin.auth_config.add_modal.host_placeholder')} | ||
/> | ||
</div> | ||
<div className="Form-group"> | ||
<label>{app.translator.trans('flarum-package-manager.admin.auth_config.add_modal.token_label')}</label> | ||
<textarea | ||
className="FormControl" | ||
oninput={(e: InputEvent) => this.token((e.target as HTMLTextAreaElement).value)} | ||
rows="6" | ||
placeholder={ | ||
this.token() === '***' | ||
? extractText(app.translator.trans('flarum-package-manager.admin.auth_config.add_modal.unchanged_token_placeholder')) | ||
: '' | ||
} | ||
> | ||
{this.token() === '***' ? '' : this.token()} | ||
</textarea> | ||
</div> | ||
<div className="Form-group"> | ||
<Button className="Button Button--primary" onclick={this.submit.bind(this)}> | ||
{app.translator.trans('flarum-package-manager.admin.auth_config.add_modal.submit_button')} | ||
</Button> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
submit() { | ||
this.attrs.onsubmit(this.type(), this.host(), this.token()); | ||
this.hide(); | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
extensions/package-manager/js/src/admin/components/ConfigureAuth.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import app from 'flarum/admin/app'; | ||
import type Mithril from 'mithril'; | ||
import ConfigureJson, { IConfigureJson } from './ConfigureJson'; | ||
import Button from 'flarum/common/components/Button'; | ||
import AuthMethodModal from './AuthMethodModal'; | ||
import extractText from 'flarum/common/utils/extractText'; | ||
|
||
export default class ConfigureAuth extends ConfigureJson<IConfigureJson> { | ||
protected type = 'auth'; | ||
|
||
title(): Mithril.Children { | ||
return app.translator.trans('flarum-package-manager.admin.auth_config.title'); | ||
} | ||
|
||
className(): string { | ||
return 'ConfigureAuth'; | ||
} | ||
|
||
content(): Mithril.Children { | ||
const authSettings = Object.keys(this.settings); | ||
|
||
return ( | ||
<div className="SettingsGroups-content"> | ||
{authSettings.length ? ( | ||
authSettings.map((type) => { | ||
const hosts = this.settings[type](); | ||
|
||
return ( | ||
<div className="Form-group"> | ||
<label>{app.translator.trans(`flarum-package-manager.admin.auth_config.types.${type}`)}</label> | ||
<div className="ConfigureAuth-hosts"> | ||
{Object.keys(hosts).map((host) => { | ||
const data = hosts[host] as string | Record<string, string>; | ||
|
||
return ( | ||
<div className="ButtonGroup ButtonGroup--full"> | ||
<Button | ||
className="Button" | ||
icon="fas fa-key" | ||
onclick={() => | ||
app.modal.show(AuthMethodModal, { | ||
type, | ||
host, | ||
token: data, | ||
onsubmit: this.onchange.bind(this), | ||
}) | ||
} | ||
> | ||
{host} | ||
</Button> | ||
<Button | ||
className="Button Button--icon" | ||
icon="fas fa-trash" | ||
aria-label={app.translator.trans('flarum-package-manager.admin.auth_config.delete_label')} | ||
onclick={() => { | ||
if (confirm(extractText(app.translator.trans('flarum-package-manager.admin.auth_config.delete_confirmation')))) { | ||
const newType = { ...this.setting(type)() }; | ||
delete newType[host]; | ||
|
||
if (Object.keys(newType).length) { | ||
this.setting(type)(newType); | ||
} else { | ||
delete this.settings[type]; | ||
} | ||
} | ||
}} | ||
/> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
</div> | ||
); | ||
}) | ||
) : ( | ||
<span className="helpText">{app.translator.trans('flarum-package-manager.admin.auth_config.no_auth_methods_configured')}</span> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
submitButton(): Mithril.Children[] { | ||
const items = super.submitButton(); | ||
|
||
items.push( | ||
<Button | ||
className="Button" | ||
loading={this.loading} | ||
onclick={() => | ||
app.modal.show(AuthMethodModal, { | ||
onsubmit: this.onchange.bind(this), | ||
}) | ||
} | ||
> | ||
{app.translator.trans('flarum-package-manager.admin.auth_config.add_button_label')} | ||
</Button> | ||
); | ||
|
||
return items; | ||
} | ||
|
||
onchange(type: string, host: string, token: string) { | ||
this.setting(type)({ ...this.setting(type)(), [host]: token }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.