forked from boreq/bolt-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
351 additions
and
21 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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,53 @@ | ||
package auth | ||
|
||
import ( | ||
"github.com/boreq/errors" | ||
"github.com/boreq/velo/domain/auth" | ||
"github.com/boreq/velo/domain/permissions" | ||
) | ||
|
||
var ErrUpdatingProfileForbidden = errors.New("this user can not update this profile") | ||
|
||
type UpdateProfile struct { | ||
Username string | ||
DisplayName auth.ValidatedDisplayName | ||
AsUser *auth.ReadUser | ||
} | ||
|
||
type UpdateProfileHandler struct { | ||
transactionProvider TransactionProvider | ||
} | ||
|
||
func NewUpdateProfileHandler( | ||
transactionProvider TransactionProvider, | ||
) *UpdateProfileHandler { | ||
return &UpdateProfileHandler{ | ||
transactionProvider: transactionProvider, | ||
} | ||
} | ||
|
||
func (h *UpdateProfileHandler) Execute(cmd UpdateProfile) error { | ||
if cmd.DisplayName.IsZero() { | ||
return errors.New("zero value of display name") | ||
} | ||
|
||
return h.transactionProvider.Write(func(r *TransactableRepositories) error { | ||
u, err := r.Users.Get(cmd.Username) | ||
if err != nil { | ||
return errors.Wrap(err, "could not get the user") | ||
} | ||
|
||
ok, err := permissions.CanUpdateProfile(toReadUser(*u), cmd.AsUser) | ||
if err != nil { | ||
return errors.Wrap(err, "permission check failed") | ||
} | ||
|
||
if !ok { | ||
return ErrUpdatingProfileForbidden | ||
} | ||
|
||
u.DisplayName = cmd.DisplayName.String() | ||
|
||
return r.Users.Put(*u) | ||
}) | ||
} |
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
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,40 @@ | ||
package auth | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
const maxDisplayNameLen = 100 | ||
|
||
type ValidatedDisplayName struct { | ||
displayName string | ||
} | ||
|
||
func NewValidatedDisplayName(displayName string) (ValidatedDisplayName, error) { | ||
if displayName == "" { | ||
return ValidatedDisplayName{}, errors.New("display name can't be empty") | ||
} | ||
|
||
if len(displayName) > maxDisplayNameLen { | ||
return ValidatedDisplayName{}, fmt.Errorf("display name length can't exceed %d characters", maxDisplayNameLen) | ||
} | ||
|
||
return ValidatedDisplayName{displayName}, nil | ||
} | ||
|
||
func MustNewValidatedDisplayName(displayName string) ValidatedDisplayName { | ||
v, err := NewValidatedDisplayName(displayName) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return v | ||
} | ||
|
||
func (u ValidatedDisplayName) String() string { | ||
return u.displayName | ||
} | ||
|
||
func (u ValidatedDisplayName) IsZero() bool { | ||
return u == ValidatedDisplayName{} | ||
} |
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
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.