github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/routes/panel/settings.go (about)

     1  package panel
     2  
     3  import (
     4  	"database/sql"
     5  	"net/http"
     6  	"strconv"
     7  	"strings"
     8  
     9  	c "github.com/Azareal/Gosora/common"
    10  	p "github.com/Azareal/Gosora/common/phrases"
    11  )
    12  
    13  func Settings(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError {
    14  	basePage, ferr := buildBasePage(w, r, u, "settings", "settings")
    15  	if ferr != nil {
    16  		return ferr
    17  	}
    18  	if !u.Perms.EditSettings {
    19  		return c.NoPermissions(w, r, u)
    20  	}
    21  
    22  	// TODO: What if the list gets too long? How should we structure this?
    23  	settings, err := basePage.Settings.BypassGetAll()
    24  	if err != nil {
    25  		return c.InternalError(err, w, r)
    26  	}
    27  	settingPhrases := p.GetAllSettingPhrases()
    28  
    29  	var settingList []*c.PanelSetting
    30  	for _, settingPtr := range settings {
    31  		s := settingPtr.Copy()
    32  		switch s.Type {
    33  		case "list":
    34  			llist := settingPhrases[s.Name+"_label"]
    35  			labels := strings.Split(llist, ",")
    36  			conv, err := strconv.Atoi(s.Content)
    37  			if err != nil {
    38  				return c.LocalError("The setting '"+s.Name+"' can't be converted to an integer", w, r, u)
    39  			}
    40  			s.Content = labels[conv-1]
    41  			// TODO: Localise this
    42  		case "bool":
    43  			if s.Content == "1" {
    44  				//s.Content = "Yes"
    45  				s.Content = p.GetTmplPhrase("option_yes")
    46  			} else {
    47  				//s.Content = "No"
    48  				s.Content = p.GetTmplPhrase("option_no")
    49  			}
    50  		case "html-attribute":
    51  			s.Type = "textarea"
    52  		}
    53  		settingList = append(settingList, &c.PanelSetting{s, p.GetSettingPhrase(s.Name)})
    54  	}
    55  
    56  	pi := c.PanelPage{basePage, tList, settingList}
    57  	return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "", "", "panel_settings", &pi})
    58  }
    59  
    60  func SettingEdit(w http.ResponseWriter, r *http.Request, u *c.User, sname string) c.RouteError {
    61  	basePage, ferr := buildBasePage(w, r, u, "edit_setting", "settings")
    62  	if ferr != nil {
    63  		return ferr
    64  	}
    65  	if !u.Perms.EditSettings {
    66  		return c.NoPermissions(w, r, u)
    67  	}
    68  
    69  	s, err := basePage.Settings.BypassGet(sname)
    70  	if err == sql.ErrNoRows {
    71  		return c.LocalError("The setting you want to edit doesn't exist.", w, r, u)
    72  	} else if err != nil {
    73  		return c.InternalError(err, w, r)
    74  	}
    75  
    76  	var itemList []c.OptionLabel
    77  	if s.Type == "list" {
    78  		llist := p.GetSettingPhrase(s.Name + "_label")
    79  		conv, err := strconv.Atoi(s.Content)
    80  		if err != nil {
    81  			return c.LocalError("The value of this setting couldn't be converted to an integer", w, r, u)
    82  		}
    83  		for index, label := range strings.Split(llist, ",") {
    84  			itemList = append(itemList, c.OptionLabel{
    85  				Label:    label,
    86  				Value:    index + 1,
    87  				Selected: conv == (index + 1),
    88  			})
    89  		}
    90  	} else if s.Type == "html-attribute" {
    91  		s.Type = "textarea"
    92  	}
    93  
    94  	pSetting := &c.PanelSetting{s, p.GetSettingPhrase(s.Name)}
    95  	pi := c.PanelSettingPage{basePage, itemList, pSetting}
    96  	return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "", "", "panel_setting", &pi})
    97  }
    98  
    99  func SettingEditSubmit(w http.ResponseWriter, r *http.Request, u *c.User, name string) c.RouteError {
   100  	headerLite, ferr := c.SimplePanelUserCheck(w, r, u)
   101  	if ferr != nil {
   102  		return ferr
   103  	}
   104  	if !u.Perms.EditSettings {
   105  		return c.NoPermissions(w, r, u)
   106  	}
   107  
   108  	name = c.SanitiseSingleLine(name)
   109  	content := c.SanitiseBody(r.PostFormValue("value"))
   110  	rerr := headerLite.Settings.Update(name, content)
   111  	if rerr != nil {
   112  		return rerr
   113  	}
   114  	// TODO: Avoid this hack
   115  	err := c.AdminLogs.Create(name, 0, "setting", u.GetIP(), u.ID)
   116  	if err != nil {
   117  		return c.InternalError(err, w, r)
   118  	}
   119  
   120  	http.Redirect(w, r, "/panel/settings/", http.StatusSeeOther)
   121  	return nil
   122  }