fix: add user popup is submittable again

react-form-hooks no longer played nice with custom input. created
a third input type `FormInput` that is made to play well
with the react-form-hooks.

also fixes auto complete overriding bg + text color on inputs.
This commit is contained in:
Jordan Knott
2021-10-06 19:03:38 -05:00
parent 8b1de30204
commit aa84cbabb2
6 changed files with 292 additions and 50 deletions

View File

@ -59,6 +59,7 @@ type Querier interface {
DeleteTeamMember(ctx context.Context, arg DeleteTeamMemberParams) error
DeleteUserAccountByID(ctx context.Context, userID uuid.UUID) error
DeleteUserAccountInvitedForEmail(ctx context.Context, email string) error
DoesUserExist(ctx context.Context, arg DoesUserExistParams) (bool, error)
GetActivityForTaskID(ctx context.Context, taskID uuid.UUID) ([]TaskActivity, error)
GetAllNotificationsForUserID(ctx context.Context, notifierID uuid.UUID) ([]Notification, error)
GetAllOrganizations(ctx context.Context) ([]Organization, error)

View File

@ -63,6 +63,9 @@ SELECT EXISTS(SELECT 1 FROM user_account WHERE username != 'system');
-- name: HasActiveUser :one
SELECT EXISTS(SELECT 1 FROM user_account WHERE username != 'system' AND active = true);
-- name: DoesUserExist :one
SELECT EXISTS(SELECT 1 FROM user_account WHERE email = $1 OR username = $2);
-- name: CreateConfirmToken :one
INSERT INTO user_account_confirm_token (email) VALUES ($1) RETURNING *;

View File

@ -157,6 +157,22 @@ func (q *Queries) DeleteUserAccountInvitedForEmail(ctx context.Context, email st
return err
}
const doesUserExist = `-- name: DoesUserExist :one
SELECT EXISTS(SELECT 1 FROM user_account WHERE email = $1 OR username = $2)
`
type DoesUserExistParams struct {
Email string `json:"email"`
Username string `json:"username"`
}
func (q *Queries) DoesUserExist(ctx context.Context, arg DoesUserExistParams) (bool, error) {
row := q.db.QueryRowContext(ctx, doesUserExist, arg.Email, arg.Username)
var exists bool
err := row.Scan(&exists)
return exists, err
}
const getAllUserAccounts = `-- name: GetAllUserAccounts :many
SELECT user_id, created_at, email, username, password_hash, profile_bg_color, full_name, initials, profile_avatar_url, role_code, bio, active FROM user_account WHERE username != 'system'
`

View File

@ -872,22 +872,26 @@ func (r *mutationResolver) DeleteTeam(ctx context.Context, input DeleteTeam) (*D
}
func (r *mutationResolver) CreateTeam(ctx context.Context, input NewTeam) (*db.Team, error) {
_, ok := GetUser(ctx)
userID, ok := GetUserID(ctx)
if !ok {
return &db.Team{}, nil
}
// if role == auth.RoleAdmin { // TODO: add permision check
if true {
createdAt := time.Now().UTC()
team, err := r.Repository.CreateTeam(ctx, db.CreateTeamParams{OrganizationID: input.OrganizationID, CreatedAt: createdAt, Name: input.Name})
return &team, err
role, err := r.Repository.GetRoleForUserID(ctx, userID)
if err != nil {
log.WithError(err).Error("while creating team")
return &db.Team{}, nil
}
return &db.Team{}, &gqlerror.Error{
Message: "You must be an organization admin to create new teams",
Extensions: map[string]interface{}{
"code": "1-400",
},
if ConvertToRoleCode(role.Code) != RoleCodeAdmin {
return &db.Team{}, &gqlerror.Error{
Message: "Must be an organization admin",
Extensions: map[string]interface{}{
"code": "0-400",
},
}
}
createdAt := time.Now().UTC()
team, err := r.Repository.CreateTeam(ctx, db.CreateTeamParams{OrganizationID: input.OrganizationID, CreatedAt: createdAt, Name: input.Name})
return &team, err
}
func (r *mutationResolver) CreateTeamMember(ctx context.Context, input CreateTeamMember) (*CreateTeamMemberPayload, error) {
@ -954,12 +958,16 @@ func (r *mutationResolver) DeleteTeamMember(ctx context.Context, input DeleteTea
}
func (r *mutationResolver) CreateUserAccount(ctx context.Context, input NewUserAccount) (*db.UserAccount, error) {
_, ok := GetUser(ctx)
userID, ok := GetUserID(ctx)
if !ok {
return &db.UserAccount{}, nil
}
// if role != auth.RoleAdmin { TODO: add permsion check
if true {
role, err := r.Repository.GetRoleForUserID(ctx, userID)
if err != nil {
log.WithError(err).Error("while creating user account")
return &db.UserAccount{}, nil
}
if ConvertToRoleCode(role.Code) != RoleCodeAdmin {
return &db.UserAccount{}, &gqlerror.Error{
Message: "Must be an organization admin",
Extensions: map[string]interface{}{
@ -972,6 +980,19 @@ func (r *mutationResolver) CreateUserAccount(ctx context.Context, input NewUserA
if err != nil {
return &db.UserAccount{}, err
}
userExists, err := r.Repository.DoesUserExist(ctx, db.DoesUserExistParams{Username: input.Username, Email: input.Email})
if err != nil {
return &db.UserAccount{}, err
}
if userExists {
return &db.UserAccount{}, &gqlerror.Error{
Message: "User with that username or email already exists",
Extensions: map[string]interface{}{
"code": "0-300",
},
}
}
userAccount, err := r.Repository.CreateUserAccount(ctx, db.CreateUserAccountParams{
FullName: input.FullName,
RoleCode: input.RoleCode,
@ -986,16 +1007,20 @@ func (r *mutationResolver) CreateUserAccount(ctx context.Context, input NewUserA
}
func (r *mutationResolver) DeleteUserAccount(ctx context.Context, input DeleteUserAccount) (*DeleteUserAccountPayload, error) {
_, ok := GetUser(ctx)
userID, ok := GetUserID(ctx)
if !ok {
return &DeleteUserAccountPayload{Ok: false}, nil
}
// if role != auth.RoleAdmin { TODO: add permision check
if true {
return &DeleteUserAccountPayload{Ok: false}, &gqlerror.Error{
Message: "User not found",
role, err := r.Repository.GetRoleForUserID(ctx, userID)
if err != nil {
log.WithError(err).Error("while deleting user account")
return &DeleteUserAccountPayload{}, nil
}
if ConvertToRoleCode(role.Code) != RoleCodeAdmin {
return &DeleteUserAccountPayload{}, &gqlerror.Error{
Message: "Must be an organization admin",
Extensions: map[string]interface{}{
"code": "0-401",
"code": "0-400",
},
}
}
@ -1004,8 +1029,6 @@ func (r *mutationResolver) DeleteUserAccount(ctx context.Context, input DeleteUs
return &DeleteUserAccountPayload{Ok: false}, err
}
// TODO(jordanknott) migrate admin ownership
err = r.Repository.DeleteUserAccountByID(ctx, input.UserID)
if err != nil {
return &DeleteUserAccountPayload{Ok: false}, err
@ -1062,16 +1085,20 @@ func (r *mutationResolver) UpdateUserPassword(ctx context.Context, input UpdateU
}
func (r *mutationResolver) UpdateUserRole(ctx context.Context, input UpdateUserRole) (*UpdateUserRolePayload, error) {
_, ok := GetUser(ctx)
userID, ok := GetUserID(ctx)
if !ok {
return &UpdateUserRolePayload{}, nil
}
// if role != auth.RoleAdmin { TODO: add permision check
if true {
role, err := r.Repository.GetRoleForUserID(ctx, userID)
if err != nil {
log.WithError(err).Error("while updating user role")
return &UpdateUserRolePayload{}, nil
}
if ConvertToRoleCode(role.Code) != RoleCodeAdmin {
return &UpdateUserRolePayload{}, &gqlerror.Error{
Message: "User not found",
Message: "Must be an organization admin",
Extensions: map[string]interface{}{
"code": "0-401",
"code": "0-400",
},
}
}
@ -1331,14 +1358,7 @@ func (r *queryResolver) Projects(ctx context.Context, input *ProjectsFilter) ([]
var teams []db.Team
var err error
/* TODO: add permsion check
if orgRole == "admin" {
teams, err = r.Repository.GetAllTeams(ctx)
} else {
teams, err = r.Repository.GetTeamsForUserIDWhereAdmin(ctx, userID)
}
*/
teams, err = r.Repository.GetAllTeams(ctx)
teams, err = r.Repository.GetTeamsForUserIDWhereAdmin(ctx, userID)
projects := make(map[string]db.Project)
for _, team := range teams {
@ -1390,13 +1410,6 @@ func (r *queryResolver) Teams(ctx context.Context) ([]db.Team, error) {
return []db.Team{}, errors.New("internal error")
}
/*
TODO: add permision check
if orgRole == "admin" {
return r.Repository.GetAllTeams(ctx)
}
*/
teams := make(map[string]db.Team)
adminTeams, err := r.Repository.GetTeamsForUserIDWhereAdmin(ctx, userID)
if err != nil {