Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

enhancement: add user email on users API (#655)

Guilherme Oenning2955293fce93
diff --git a/app/handlers/apiv1/post.go b/app/handlers/apiv1/post.go
index fb6a9ac30..39ebb9b13 100644
--- a/app/handlers/apiv1/post.go
+++ b/app/handlers/apiv1/post.go
@@ -282,6 +282,10 @@ func ListVoters() web.HandlerFunc {
return c.Failure(err)
}
+ for _, u := range users {
+ u.ShowEmail = c.IsAuthenticated() && c.User().IsCollaborator()
+ }
+
return c.Ok(users)
}
}
diff --git a/app/handlers/apiv1/user.go b/app/handlers/apiv1/user.go
index f52616ffd..47d86381d 100644
--- a/app/handlers/apiv1/user.go
+++ b/app/handlers/apiv1/user.go
@@ -16,6 +16,10 @@ func ListUsers() web.HandlerFunc {
return c.Failure(err)
}
+ for _, u := range users {
+ u.ShowEmail = c.IsAuthenticated() && c.User().IsCollaborator()
+ }
+
return c.Ok(users)
}
}
diff --git a/app/models/identity.go b/app/models/identity.go
index 30bf8cb99..0847cc47d 100644
--- a/app/models/identity.go
+++ b/app/models/identity.go
@@ -1,6 +1,7 @@
package models
import (
+ "encoding/json"
"time"
"github.com/getfider/fider/app/pkg/jwt"
@@ -39,13 +40,36 @@ type Upload struct {
//User represents an user inside our application
type User struct {
- ID int `json:"id"`
- Name string `json:"name"`
- Email string `json:"-"`
- Tenant *Tenant `json:"-"`
- Role Role `json:"role"`
- Providers []*UserProvider `json:"-"`
- Status UserStatus `json:"status"`
+ ShowEmail bool
+ ID int
+ Name string
+ Email string
+ Tenant *Tenant
+ Role Role
+ Providers []*UserProvider
+ Status UserStatus
+}
+
+// MarshalJSON interface redefinition
+func (u User) MarshalJSON() ([]byte, error) {
+ email := ""
+ if u.ShowEmail {
+ email = u.Email
+ }
+
+ return json.Marshal(&struct {
+ ID int `json:"id"`
+ Name string `json:"name"`
+ Email string `json:"email,omitempty"`
+ Role Role `json:"role"`
+ Status UserStatus `json:"status"`
+ }{
+ ID: u.ID,
+ Name: u.Name,
+ Email: email,
+ Role: u.Role,
+ Status: u.Status,
+ })
}
//UserStatus is the status of a user