Skip to content

Commit

Permalink
Storage functions for org users association (#6741)
Browse files Browse the repository at this point in the history
* Storage functions for org users association

Signed-off-by: sonali wale <sonali.wale@progress.com>

* Minor changes added

Signed-off-by: sonali wale <sonali.wale@progress.com>

* Minor changes added

Signed-off-by: sonali wale <sonali.wale@progress.com>
  • Loading branch information
sonali523 authored and vinay033 committed Apr 22, 2022
1 parent 02e37d3 commit 013e0fa
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,66 @@ func (p *postgres) getUsers(ctx context.Context, q querier, serverID string) ([]
return users, nil
}

// StoreOrgUserAssociation: Inserts an entry of org users
func (p *postgres) StoreOrgUserAssociation(ctx context.Context, serverID, orgID, username string, isAdmin bool) (storage.OrgUser, error) {
return p.insertOrgUserAssociation(ctx, serverID, orgID, username, isAdmin)
}

func (p *postgres) insertOrgUserAssociation(ctx context.Context, serverID, orgID, username string, isAdmin bool) (storage.OrgUser, error) {
var orgUser storage.OrgUser
nowTime := time.Now()
err := p.db.QueryRowContext(ctx,
`INSERT INTO org_users (
org_id, user_id,
is_admin,
created_at,updated_at)
VALUES ($1, SELECT id from users where infra_server_username=$2 and server_id=$3, $4, $5, $5)
RETURNING id, org_id, user_id, is_admin, created_at, updated_at`,
orgID, username, serverID, isAdmin, nowTime).
Scan(&orgUser.ID, &orgUser.OrgId, &orgUser.UserID, &orgUser.IsAdmin, &orgUser.CreatedAt, &orgUser.UpdatedAt)
if err != nil {
return storage.OrgUser{}, p.processError(err)
}
return orgUser, nil
}

// EditOrgUserAssociation: Updates an entry of org users
func (p *postgres) EditOrgUserAssociation(ctx context.Context, serverID, orgID, username string, isAdmin bool) (storage.OrgUser, error) {
return p.updateOrgUserAssociation(ctx, serverID, orgID, username, isAdmin)
}

func (p *postgres) updateOrgUserAssociation(ctx context.Context, serverID, orgID, username string, isAdmin bool) (storage.OrgUser, error) {
var orgUser storage.OrgUser
nowTime := time.Now()
err := p.db.QueryRowContext(ctx,
`UPDATE org_users SET
is_admin = $1,
updated_at= $2
WHERE org_id=$3 and user_id = SELECT id FROM users u WHERE u.infra_server_username=$4 and u.server_id=$5
RETURNING id, org_id, user_id, is_admin, created_at, updated_at`,
isAdmin, nowTime, orgID, username, serverID).
Scan(&orgUser.ID, &orgUser.OrgId, &orgUser.UserID, &orgUser.IsAdmin, &orgUser.CreatedAt, &orgUser.UpdatedAt)
if err != nil {
return storage.OrgUser{}, p.processError(err)
}
return orgUser, nil
}

// DeleteOrgUserAssociation: Deletes an entry from org users
func (p *postgres) DeleteOrgUserAssociation(ctx context.Context, serverID, orgID, username string, isAdmin bool) (storage.OrgUser, error) {
return p.deleteOrgUserAssociation(ctx, serverID, orgID, username, isAdmin)
}

func (p *postgres) deleteOrgUserAssociation(ctx context.Context, serverID, orgID, username string, isAdmin bool) (storage.OrgUser, error) {
var orgUser storage.OrgUser
err := p.db.QueryRowContext(ctx,
`DELETE FROM org_users
WHERE org_id=$1 and user_id = SELECT id FROM users u WHERE u.infra_server_username=$2 and u.server_id=$3
RETURNING id, org_id, user_id, is_admin, created_at, updated_at`,
orgID, username, serverID).
Scan(&orgUser.ID, &orgUser.OrgId, &orgUser.UserID, &orgUser.IsAdmin, &orgUser.CreatedAt, &orgUser.UpdatedAt)
if err != nil {
return storage.OrgUser{}, p.processError(err)
}
return orgUser, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ CREATE INDEX IF NOT EXISTS users_automate_user_id_index ON users (automate_user_

-- create table org_users
CREATE TABLE IF NOT EXISTS org_users (
id TEXT PRIMARY KEY,
org_id TEXT NOT NULL,
user_id TEXT NOT NULL references users(id) ON DELETE RESTRICT,
is_admin BOOLEAN NOT NULL DEFAULT FALSE
id SERIAL PRIMARY KEY,
org_id TEXT NOT NULL,
user_id TEXT NOT NULL references users(id) ON DELETE CASCADE,
is_admin BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX IF NOT EXISTS org_users_org_id_index ON org_users (org_id);
CREATE INDEX IF NOT EXISTS org_users_user_id_index ON org_users (user_id);
13 changes: 11 additions & 2 deletions components/infra-proxy-service/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ type Storage interface {
EditOrg(ctx context.Context, id string, name string, adminUser string, serverID string, projects []string) (Org, error)
TouchOrg(ctx context.Context, id string, serverID string) (Org, error)

InsertUser(ctx context.Context, id, serverID, infraServerUsername, credentialID, Connector, automateUserID string, IsServerAdmin bool) (User, error)
InsertUser(ctx context.Context, id, serverID, infraServerUsername, credentialID, connector, automateUserID string, isServerAdmin bool) (User, error)
GetUser(ctx context.Context, id string) (User, error)
EditUser(ctx context.Context, id, serverID, infraServerUsername, credentialID, Connector, automateUserID string, IsServerAdmin bool) (User, error)
EditUser(ctx context.Context, id, serverID, infraServerUsername, credentialID, connector, automateUserID string, isServerAdmin bool) (User, error)
DeleteUser(ctx context.Context, id string) (User, error)

GetAutomateInfraServerUsers(ctx context.Context, serverId string) ([]User, error)
GetAutomateOrgUsers(ctx context.Context, orgId string) ([]OrgUser, error)

StoreOrgUserAssociation(ctx context.Context, serverID, orgID, username string, isAdmin bool) (OrgUser, error)
EditOrgUserAssociation(ctx context.Context, serverID, orgID, username string, isAdmin bool) (OrgUser, error)
DeleteOrgUserAssociation(ctx context.Context, serverID, orgID, username string, isAdmin bool) (OrgUser, error)
}

type MigrationStorage interface {
Expand Down Expand Up @@ -130,9 +135,13 @@ type User struct {
}

type OrgUser struct {
ID int
OrgId string
UserID int
IsAdmin bool
InfraServerUsername string
CreatedAt time.Time
UpdatedAt time.Time
}

type Migration struct {
Expand Down

0 comments on commit 013e0fa

Please sign in to comment.