mirror of
https://github.com/writeas/writeas-cli
synced 2025-07-26 23:08:16 +00:00
update requireAuth helper, check logged in users
this uses the usersLoggedIn helper to check for already logged in users, selecting the single user when only one present and returning and error when multiple are logged in.
This commit is contained in:
parent
58dd3f985a
commit
630a867a34
@ -15,9 +15,25 @@ import (
|
|||||||
|
|
||||||
func requireAuth(f cli.ActionFunc, action string) cli.ActionFunc {
|
func requireAuth(f cli.ActionFunc, action string) cli.ActionFunc {
|
||||||
return func(c *cli.Context) error {
|
return func(c *cli.Context) error {
|
||||||
|
// check for logged in users when host is provided without user
|
||||||
|
if c.GlobalIsSet("host") && !c.GlobalIsSet("user") {
|
||||||
|
// multiple users should display a list
|
||||||
|
if num, users, err := usersLoggedIn(c); num > 1 && err == nil {
|
||||||
|
return cli.NewExitError(fmt.Sprintf("Multiple logged in users, please use '-u' or '-user' to specify one of:\n%s", users), 1)
|
||||||
|
} else if num == 1 && err == nil {
|
||||||
|
// single user found for host should be set as user flag so LoadUser can
|
||||||
|
// succeed, and notify the client
|
||||||
|
if err := c.GlobalSet("user", users[0]); err != nil {
|
||||||
|
return cli.NewExitError(fmt.Sprintf("Failed to set user flag for only logged in user at host %s: %v", users[0], err), 1)
|
||||||
|
}
|
||||||
|
fmt.Printf("Host specified without user flag, using logged in user: %s\n", users[0])
|
||||||
|
} else if err != nil {
|
||||||
|
return cli.NewExitError(fmt.Sprintf("Failed to check for logged in users: %v", err), 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
u, err := config.LoadUser(c)
|
u, err := config.LoadUser(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cli.NewExitError(fmt.Sprintf("couldn't load config: %v", err), 1)
|
return cli.NewExitError(fmt.Sprintf("Couldn't load user: %v", err), 1)
|
||||||
}
|
}
|
||||||
if u == nil {
|
if u == nil {
|
||||||
return cli.NewExitError("You must be authenticated to "+action+".\nLog in first with: "+executable.Name()+" auth <username>", 1)
|
return cli.NewExitError("You must be authenticated to "+action+".\nLog in first with: "+executable.Name()+" auth <username>", 1)
|
||||||
|
@ -148,7 +148,7 @@ func main() {
|
|||||||
Name: "posts",
|
Name: "posts",
|
||||||
Usage: "List all of your posts",
|
Usage: "List all of your posts",
|
||||||
Description: "This will list only local posts.",
|
Description: "This will list only local posts.",
|
||||||
Action: commands.CmdListPosts,
|
Action: requireAuth(commands.CmdListPosts, "posts"),
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
Name: "id",
|
Name: "id",
|
||||||
@ -170,7 +170,7 @@ func main() {
|
|||||||
}, {
|
}, {
|
||||||
Name: "blogs",
|
Name: "blogs",
|
||||||
Usage: "List blogs",
|
Usage: "List blogs",
|
||||||
Action: commands.CmdCollections,
|
Action: requireAuth(commands.CmdCollections, "blogs"),
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
Name: "tor, t",
|
Name: "tor, t",
|
||||||
@ -189,7 +189,7 @@ func main() {
|
|||||||
}, {
|
}, {
|
||||||
Name: "claim",
|
Name: "claim",
|
||||||
Usage: "Claim local unsynced posts",
|
Usage: "Claim local unsynced posts",
|
||||||
Action: commands.CmdClaim,
|
Action: requireAuth(commands.CmdClaim, "claim"),
|
||||||
Description: "This will claim any unsynced posts local to this machine. To see which posts these are run: wf posts.",
|
Description: "This will claim any unsynced posts local to this machine. To see which posts these are run: wf posts.",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
@ -229,7 +229,7 @@ func main() {
|
|||||||
{
|
{
|
||||||
Name: "logout",
|
Name: "logout",
|
||||||
Usage: "Log out of a WriteFreely instance",
|
Usage: "Log out of a WriteFreely instance",
|
||||||
Action: cmdLogOut,
|
Action: requireAuth(cmdLogOut, "logout"),
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
Name: "tor, t",
|
Name: "tor, t",
|
||||||
|
@ -357,18 +357,18 @@ func CmdClaim(c *cli.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CmdAuth(c *cli.Context) error {
|
func CmdAuth(c *cli.Context) error {
|
||||||
|
username := c.Args().Get(0)
|
||||||
// Check configuration
|
// Check configuration
|
||||||
u, err := config.LoadUser(c)
|
u, err := config.LoadUser(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cli.NewExitError(fmt.Sprintf("couldn't load config: %v", err), 1)
|
return cli.NewExitError(fmt.Sprintf("couldn't load config: %v", err), 1)
|
||||||
}
|
}
|
||||||
if u != nil && u.AccessToken != "" {
|
if u != nil && u.AccessToken != "" && username == u.User.Username {
|
||||||
return cli.NewExitError("You're already authenticated as "+u.User.Username+". Log out with: "+executable.Name()+" logout", 1)
|
return cli.NewExitError("You're already authenticated as "+u.User.Username, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate arguments and get password
|
// Validate arguments and get password
|
||||||
// TODO: after global config, check for default user
|
// TODO: after global config, check for default user
|
||||||
username := c.Args().Get(0)
|
|
||||||
if username == "" {
|
if username == "" {
|
||||||
return cli.NewExitError("usage: "+executable.Name()+" auth <username>", 1)
|
return cli.NewExitError("usage: "+executable.Name()+" auth <username>", 1)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user