Browse Source

Correct CP redirects

Vova Tkach 6 years ago
parent
commit
27889ec630
2 changed files with 22 additions and 0 deletions
  1. 14 0
      engine/engine.go
  2. 8 0
      utils/utils.go

+ 14 - 0
engine/engine.go

@@ -95,6 +95,13 @@ func (this *Engine) Process() bool {
 
 	// Show login page if need
 	if this.Wrap.S.GetInt("UserId", 0) <= 0 {
+		// Redirect to main url if needs
+		if this.Wrap.R.URL.Path != "/cp/" {
+			http.Redirect(this.Wrap.W, this.Wrap.R, "/cp/"+utils.ExtractGetParams(this.Wrap.R.RequestURI), 302)
+			return true
+		}
+
+		// Show login form
 		utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpLogin, nil)
 		return true
 	}
@@ -107,6 +114,13 @@ func (this *Engine) Process() bool {
 
 	// Only active admins can use backend
 	if !(this.Wrap.User.A_admin == 1 && this.Wrap.User.A_active == 1) {
+		// Redirect to main url if needs
+		if this.Wrap.R.URL.Path != "/cp/" {
+			http.Redirect(this.Wrap.W, this.Wrap.R, "/cp/"+utils.ExtractGetParams(this.Wrap.R.RequestURI), 302)
+			return true
+		}
+
+		// Show login form
 		utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpLogin, nil)
 		return true
 	}

+ 8 - 0
utils/utils.go

@@ -234,3 +234,11 @@ func UnixTimestampToMySqlDateTime(sec int64) string {
 func UnixTimestampToFormat(sec int64, format string) string {
 	return time.Unix(sec, 0).Format(format)
 }
+
+func ExtractGetParams(str string) string {
+	i := strings.Index(str, "?")
+	if i == -1 {
+		return ""
+	}
+	return "?" + str[i+1:]
+}