Open platform to collect and prioritize feedback
refactor: expose role as string (#524)
Guilherme Oenningaac27cd60724
diff --git a/app/models/identity.go b/app/models/identity.goindex e45141331..44f32c69e 100644--- a/app/models/identity.go+++ b/app/models/identity.go@@ -1,6 +1,7 @@package modelsimport (+ "fmt""time""github.com/getfider/fider/app/pkg/jwt"@@ -65,6 +66,29 @@ const (RoleAdministrator Role = 3)+var roleIDs = map[Role]string{+ RoleVisitor: "visitor",+ RoleCollaborator: "collaborator",+ RoleAdministrator: "administrator",+}++var roleNames = map[string]Role{+ "visitor": RoleVisitor,+ "collaborator": RoleCollaborator,+ "administrator": RoleAdministrator,+}++// MarshalJSON returns the JSON version of the role+func (role Role) MarshalJSON() ([]byte, error) {+ return []byte(fmt.Sprintf(`"%s"`, roleIDs[role])), nil+}++// UnmarshalString parse string into a role+func (role *Role) UnmarshalString(s string) error {+ *role = roleNames[s]+ return nil+}+//EmailVerificationKind specifies which kind of process is being verified by emailtype EmailVerificationKind int16diff --git a/app/pkg/web/binder.go b/app/pkg/web/binder.goindex e2ce8c4ed..59a097ae6 100644--- a/app/pkg/web/binder.go+++ b/app/pkg/web/binder.go@@ -58,8 +58,19 @@ func (b *DefaultBinder) bindRoute(idx int, target reflect.Value, targetType reflfieldTypeKind := field.Type().Kind()if isInt(fieldTypeKind) {- value, _ := strconv.ParseInt(params[name], 10, 64)- field.SetInt(value)+ value, err := strconv.ParseInt(params[name], 10, 64)+ if err != nil {+ obj := reflect.New(field.Type())+ m := obj.MethodByName("UnmarshalString")+ if m.IsValid() {+ r := m.Call([]reflect.Value{reflect.ValueOf(params[name])})+ if r[0].IsNil() {+ field.Set(obj.Elem())+ }+ }+ } else {+ field.SetInt(value)+ }} else if isString(fieldTypeKind) {field.SetString(params[name])}diff --git a/app/pkg/web/binder_test.go b/app/pkg/web/binder_test.goindex 3825a5bf6..5b8d07191 100644--- a/app/pkg/web/binder_test.go+++ b/app/pkg/web/binder_test.go@@ -176,26 +176,36 @@ func TestDefaultBinder_POST_NonJSON(t *testing.T) {Expect(err).Equals(web.ErrContentTypeNotAllowed)}-func TestDefaultBinder_POST_CustomType(t *testing.T) {- RegisterT(t)+type Size int+type Size32 int32+type Size64 int64++const (+ Small Size = 1+ Large Size = 2+ Small32 Size32 = 3+ Large32 Size32 = 4+ Small64 Size64 = 5+ Large64 Size64 = 6+)- type Size int- type Size32 int32- type Size64 int64- const (- Small Size = 1- Large Size = 2- Small32 Size32 = 3- Large32 Size32 = 4- Small64 Size64 = 5- Large64 Size64 = 6- )+type pizza struct {+ TheSize Size `route:"size"`+ TheSize32 Size32 `route:"size32"`+ TheSize64 Size64 `route:"size64"`+}- type pizza struct {- TheSize Size `route:"size"`- TheSize32 Size32 `route:"size32"`- TheSize64 Size64 `route:"size64"`+func (p *Size) UnmarshalString(s string) error {+ if s == "large" {+ *p = Large+ } else {+ *p = Small}+ return nil+}++func TestDefaultBinder_POST_CustomType(t *testing.T) {+ RegisterT(t)params := make(web.StringMap, 0)params["size"] = "1"@@ -210,3 +220,20 @@ func TestDefaultBinder_POST_CustomType(t *testing.T) {Expect(u.TheSize32).Equals(Large32)Expect(u.TheSize64).Equals(Small64)}++func TestDefaultBinder_POST_CustomType_Unmarshall(t *testing.T) {+ RegisterT(t)++ type pizza struct {+ TheSize Size `route:"size"`+ }++ params := make(web.StringMap, 0)+ params["size"] = "large"+ ctx := newBodyContext("POST", params, `{ }`, "application/json")++ u := new(pizza)+ err := binder.Bind(u, ctx)+ Expect(err).IsNil()+ Expect(u.TheSize).Equals(Large)+}diff --git a/public/components/common/Gravatar.tsx b/public/components/common/Gravatar.tsxindex 8b4d8bee3..cddbcfed5 100644--- a/public/components/common/Gravatar.tsx+++ b/public/components/common/Gravatar.tsx@@ -2,7 +2,7 @@ import "./Gravatar.scss";import * as React from "react";import { classSet, Fider } from "@fider/services";-import { User, UserRole } from "@fider/models";+import { User, isCollaborator } from "@fider/models";interface GravatarProps {user?: User;@@ -18,7 +18,7 @@ export const Gravatar = (props: GravatarProps) => {const className = classSet({"c-avatar": true,[`m-${size}`]: true,- "m-staff": props.user && props.user.role >= UserRole.Collaborator+ "m-staff": props.user && isCollaborator(props.user.role)});return <img className={className} title={name} src={url} />;diff --git a/public/components/common/UserName.tsx b/public/components/common/UserName.tsxindex 294413e82..d90cb59eb 100644--- a/public/components/common/UserName.tsx+++ b/public/components/common/UserName.tsx@@ -1,7 +1,7 @@import "./UserName.scss";import * as React from "react";-import { User, UserRole, CurrentUser } from "@fider/models";+import { User, isCollaborator } from "@fider/models";import { classSet } from "@fider/services";interface UserNameProps {@@ -11,7 +11,7 @@ interface UserNameProps {export const UserName = (props: UserNameProps) => {const className = classSet({"c-username": true,- "m-staff": props.user.role >= UserRole.Collaborator+ "m-staff": isCollaborator(props.user.role)});return <span className={className}>{props.user.name || "Anonymous"}</span>;diff --git a/public/models/identity.ts b/public/models/identity.tsindex e13b407e3..c387f85c2 100644--- a/public/models/identity.ts+++ b/public/models/identity.ts@@ -16,11 +16,15 @@ export interface User {}export enum UserRole {- Visitor = 1,- Collaborator = 2,- Administrator = 3+ Visitor = "visitor",+ Collaborator = "collaborator",+ Administrator = "administrator"}+export const isCollaborator = (role: UserRole): boolean => {+ return role === UserRole.Collaborator || role === UserRole.Administrator;+};+export interface CurrentUser {id: number;name: string;diff --git a/public/pages/Administration/pages/ManageMembers.page.tsx b/public/pages/Administration/pages/ManageMembers.page.tsxindex bf045d362..628ce8059 100644--- a/public/pages/Administration/pages/ManageMembers.page.tsx+++ b/public/pages/Administration/pages/ManageMembers.page.tsx@@ -33,7 +33,7 @@ export class ManageMembersPage extends AdminBasePage<ManageMembersPageProps, Man}private groupUsers(): ManageMembersPageState {- const usersByRole = this.props.users.reduce<{ [key: number]: User[] }>((groups, x) => {+ const usersByRole = this.props.users.reduce<{ [key: string]: User[] }>((groups, x) => {groups[x.role] = [x].concat(groups[x.role] || []);return groups;}, {});