limitless flag on accounts (#96)

This commit is contained in:
Michael Quigley 2023-01-13 13:16:10 -05:00
parent e6932d67f2
commit 4e4bd1c876
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
12 changed files with 71 additions and 10 deletions

View File

@ -100,7 +100,7 @@ func (h *enableHandler) Handle(params environment.EnableParams, principal *rest_
}
func (h *enableHandler) checkLimits(principal *rest_model_zrok.Principal, tx *sqlx.Tx) error {
if h.cfg.Environments > Unlimited {
if !principal.Limitless && h.cfg.Environments > Unlimited {
envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx)
if err != nil {
return errors.Errorf("unable to find environments for account '%v': %v", principal.Email, err)

View File

@ -143,7 +143,7 @@ func (h *shareHandler) Handle(params share.ShareParams, principal *rest_model_zr
}
func (h *shareHandler) checkLimits(principal *rest_model_zrok.Principal, envs []*store.Environment, tx *sqlx.Tx) error {
if h.cfg.Shares > Unlimited {
if !principal.Limitless && h.cfg.Shares > Unlimited {
total := 0
for i := range envs {
shrs, err := str.FindSharesForEnvironment(envs[i].Id, tx)

View File

@ -7,18 +7,19 @@ import (
type Account struct {
Model
Email string
Password string
Token string
Email string
Password string
Token string
Limitless bool
}
func (self *Store) CreateAccount(a *Account, tx *sqlx.Tx) (int, error) {
stmt, err := tx.Prepare("insert into accounts (email, password, token) values ($1, $2, $3) returning id")
stmt, err := tx.Prepare("insert into accounts (email, password, token, limitless) values ($1, $2, $3, $4) returning id")
if err != nil {
return 0, errors.Wrap(err, "error preparing accounts insert statement")
}
var id int
if err := stmt.QueryRow(a.Email, a.Password, a.Token).Scan(&id); err != nil {
if err := stmt.QueryRow(a.Email, a.Password, a.Token, a.Limitless).Scan(&id); err != nil {
return 0, errors.Wrap(err, "error executing accounts insert statement")
}
return id, nil

View File

@ -0,0 +1,25 @@
-- +migrate Up
alter table accounts rename to accounts_old;
create table accounts (
id serial primary key,
email varchar(1024) not null unique,
password char(128) not null,
token varchar(32) not null unique,
limitless boolean not null default(false),
created_at timestamp not null default(current_timestamp),
updated_at timestamp not null default(current_timestamp),
constraint chk_email check (email <> ''),
constraint chk_password check (password <> ''),
constraint chk_token check(token <> '')
);
insert into accounts(id, email, password, token, created_at, updated_at)
select id, email, password, token, created_at, updated_at from accounts_old;
alter table environments drop constraint fk_accounts_id;
alter table environments add constraint fk_accounts_id foreign key (account_id) references accounts(id);
drop table accounts_old;

View File

@ -0,0 +1,22 @@
-- +migrate Up
alter table accounts rename to accounts_old;
create table accounts (
id integer primary key,
email string not null unique,
password string not null,
token string not null unique,
limitless boolean not null default(false),
created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
constraint chk_email check (email <> ''),
constraint chk_password check (password <> ''),
constraint chk_token check(token <> '')
);
insert into accounts (id, email, password, token, created_at, updated_at)
select id, email, password, token, created_at, updated_at from accounts_old;
drop table accounts_old;

View File

@ -30,9 +30,10 @@ func (za *zrokAuthenticator) authenticate(token string) (*rest_model_zrok.Princi
if a, err := str.FindAccountWithToken(token, tx); err == nil {
principal := &rest_model_zrok.Principal{
ID: int64(a.Id),
Token: a.Token,
Email: a.Email,
ID: int64(a.Id),
Token: a.Token,
Email: a.Email,
Limitless: a.Limitless,
}
return principal, nil
} else {

View File

@ -26,6 +26,9 @@ type Principal struct {
// id
ID int64 `json:"id,omitempty"`
// limitless
Limitless bool `json:"limitless,omitempty"`
// token
Token string `json:"token,omitempty"`
}

View File

@ -943,6 +943,9 @@ func init() {
"id": {
"type": "integer"
},
"limitless": {
"type": "boolean"
},
"token": {
"type": "string"
}
@ -2111,6 +2114,9 @@ func init() {
"id": {
"type": "integer"
},
"limitless": {
"type": "boolean"
},
"token": {
"type": "string"
}

View File

@ -610,6 +610,8 @@ definitions:
type: string
token:
type: string
limitless:
type: boolean
admin:
type: boolean

View File

@ -121,6 +121,7 @@
* @property {number} id
* @property {string} email
* @property {string} token
* @property {boolean} limitless
* @property {boolean} admin
*/