diff --git a/.vscode/launch.json b/.vscode/launch.json index b8d2d84..f82900b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,5 +4,12 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "name": "Launch Amsterdam", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}" + } ] } \ No newline at end of file diff --git a/community.go b/community.go index b328d97..e5fe217 100644 --- a/community.go +++ b/community.go @@ -115,7 +115,9 @@ func ShowCommunity(ctxt ui.AmContext) (string, any, error) { } tag, err := comm.LanguageTag() if err == nil && tag != nil { - ctxt.VarMap().Set("language", display.Languages(*prefs.LanguageTag()).Name(tag)) + my_lang := prefs.LanguageTag() + disp := display.Languages(*my_lang) + ctxt.VarMap().Set("language", disp.Name(tag)) } if comm.Rules != nil && *comm.Rules != "" { ctxt.VarMap().Set("rules", *comm.Rules) diff --git a/database/community.go b/database/community.go index d450fc4..1938dbc 100644 --- a/database/community.go +++ b/database/community.go @@ -273,10 +273,10 @@ func (c *Community) SetProfileData(name string, alias string, synopsis *string, create_lvl uint16, delete_lvl uint16, join_lvl uint16) error { c.Mutex.Lock() defer c.Mutex.Unlock() - _, err := amdb.Exec(`UPDATE communities SET commname = ?, alias = ?, synopsis = ? rules = ?, language = ?, + _, err := amdb.Exec(`UPDATE communities SET commname = ?, alias = ?, synopsis = ?, rules = ?, language = ?, joinkey = ?, membersonly = ?, hide_dir = ?, hide_search = ?, read_lvl = ?, write_lvl = ?, create_lvl = ?, delete_lvl = ?, join_lvl = ?, lastupdate = NOW() WHERE commid = ?`, - name, alias, synopsis, rules, joinkey, membersonly, hideDirectory, hideSearch, read_lvl, write_lvl, + name, alias, synopsis, rules, language, joinkey, membersonly, hideDirectory, hideSearch, read_lvl, write_lvl, create_lvl, delete_lvl, join_lvl, c.Id) if err == nil { c.Name = name diff --git a/database/security.go b/database/security.go index 5a4575b..3398305 100644 --- a/database/security.go +++ b/database/security.go @@ -208,6 +208,7 @@ func (r *CfgRole) LevelStr() string { type RoleList interface { Roles() []Role Default() Role + FindForLevel(uint16) Role } func (r *CfgRoleList) Roles() []Role { @@ -222,6 +223,15 @@ func (r *CfgRoleList) Default() Role { return r.defptr } +func (r *CfgRoleList) FindForLevel(level uint16) Role { + for _, rp := range r.roleptrs { + if rp.level == level { + return rp + } + } + return nil +} + /* AmRole returns a Role given a string ID. * Parameters: * id - ID of the role to look up. diff --git a/ui/amcontext.go b/ui/amcontext.go index 6b2c688..d22525f 100644 --- a/ui/amcontext.go +++ b/ui/amcontext.go @@ -112,6 +112,12 @@ func (c *amContext) ClearSession() { // CurrentCommunity returns the current community, if one's been set. func (c *amContext) CurrentCommunity() *database.Community { + if c.community == nil { + cv, ok := c.session.Values["lastCommunity"] + if ok && !c.CurrentUser().IsAnon { + c.SetCommunityContext(fmt.Sprintf("%d", cv)) + } + } return c.community } diff --git a/ui/dialog.go b/ui/dialog.go index 01be195..263e5b4 100644 --- a/ui/dialog.go +++ b/ui/dialog.go @@ -202,6 +202,24 @@ func (fld *DialogItem) SetInt(v int) { fld.Value = fmt.Sprintf("%d", v) } +// SetLevel sets a security level into a field value. +func (fld *DialogItem) SetLevel(level uint16) { + fld.Value = fmt.Sprintf("%d", level) + if fld.Type == "rolelist" { + rolelist := database.AmRoleList(fld.Param) + fld.AuxData = rolelist.FindForLevel(level) + } +} + +// GetLevel gets a field's value as a security level. +func (fld *DialogItem) GetLevel() uint16 { + v, err := strconv.Atoi(fld.Value) + if err != nil { + return uint16(0) + } + return uint16(v) +} + // IsEmpty returns true if the field is empty. func (fld *DialogItem) IsEmpty() bool { return len(fld.Value) == 0 @@ -278,23 +296,15 @@ func (d *Dialog) Render(ctxt AmContext) (string, any, error) { } } case "rolelist": - rl := database.AmRoleList(fld.Param) - defv := rl.Default().LevelStr() - if d.Fields[i].Value == "" { - d.Fields[i].Value = defv - } else { - ok := false - for _, r := range rl.Roles() { - if d.Fields[i].Value == r.LevelStr() { - ok = true - break - } - if !ok { - d.Fields[i].Value = defv - } + if d.Fields[i].AuxData == nil { + rolelist := database.AmRoleList(fld.Param) + role := rolelist.FindForLevel(d.Fields[i].GetLevel()) + if role == nil { + role := rolelist.Default() + d.Fields[i].Value = role.LevelStr() } + d.Fields[i].AuxData = role } - // TODO: want to do something like dropdown but not sure what yet } } if d.MenuSelector != "" && d.MenuSelector != "nochange" { @@ -385,6 +395,13 @@ func (d *Dialog) LoadFromForm(ctxt AmContext) { d.Fields[i].AuxData = dvals case "userphoto", "communitylogo": d.Fields[i].Value = ctxt.FormField(fmt.Sprintf("%s_data", fld.Name)) + case "rolelist": + d.Fields[i].Value = ctxt.FormField(fld.Name) + rolelist := database.AmRoleList(d.Fields[i].Param) + role := rolelist.FindForLevel(d.Fields[i].GetLevel()) + if role != nil { + d.Fields[i].AuxData = role + } default: d.Fields[i].Value = ctxt.FormField(fld.Name) } @@ -510,6 +527,19 @@ func validateCountryField(fld *DialogItem) error { return nil } +/* validateRoleListField validates a role list field. + * Parameters: + * fld - The field to be validated. + * Returns: + * Standard Go error status. + */ +func validateRoleListField(fld *DialogItem) error { + if fld.AuxData == nil { + return fmt.Errorf("invalid role level %s found in field \"%s\"", fld.Value, fld.Caption) + } + return nil +} + /* validateDateField validates a date field. * Parameters: * fld - The field to be validated. @@ -552,7 +582,7 @@ var validators = map[string]validatorFunc{ "integer": validateIntegerField, "localelist": nilValidator, "password": validateTextField, - "rolelist": nilValidator, + "rolelist": validateRoleListField, "text": validateTextField, "tzlist": nilValidator, "userphoto": nilValidator, diff --git a/ui/dialogs/commprofile.yaml b/ui/dialogs/commprofile.yaml index a0d1f58..9c0b38b 100644 --- a/ui/dialogs/commprofile.yaml +++ b/ui/dialogs/commprofile.yaml @@ -24,7 +24,7 @@ fields: caption: "Community Name" required: true size: 32 - maxLength: 128 + maxlength: 128 - type: "ams_id" name: "alias" caption: "Community Alias" @@ -35,12 +35,12 @@ fields: name: "synopsis" caption: "Synopsis" size: 32 - maxLength: 255 + maxlength: 255 - type: "text" name: "rules" caption: "Rules" size: 32 - maxLength: 255 + maxlength: 255 - type: "localelist" name: "language" caption: "Primary Language" @@ -114,7 +114,7 @@ fields: caption: "Join Key" subcaption: "(for private communities)" size: 32 - maxLength: 64 + maxlength: 64 - type: "checkbox" name: "membersonly" caption: "Allow only members to access this community" diff --git a/ui/views/comprofile.jet b/ui/views/comprofile.jet index e3f0af2..b4c532b 100644 --- a/ui/views/comprofile.jet +++ b/ui/views/comprofile.jet @@ -114,8 +114,8 @@ {{ if isset(homePage) }}
Homepage: - {{ homepage }} + {{ homePage }}
{{ end }}