Browse Source

User mod, new fields, CP access, checkbox, db upgrade

Vova Tkach 6 years ago
parent
commit
a88ab08ba5

+ 6 - 0
assets/cp.styles.css

@@ -283,4 +283,10 @@ body.cp .wrap .sidebar.sidebar-left ul.nav li.nav-item svg.sicon {
 
 .data-form textarea {
 	min-height: 5.4rem;
+}
+
+.data-form input[type=checkbox].form-control {
+	cursor: pointer;
+	display: inline-block;
+	width: calc(2.25rem + 0px);
 }

File diff suppressed because it is too large
+ 0 - 0
assets/cp.styles.css.go


+ 1 - 1
consts/consts.go

@@ -6,7 +6,7 @@ import (
 
 const Debug = true
 const ServerVersion = "1.0.0"
-const AssetsVersion = "1"
+const AssetsVersion = "2"
 const AssetsPath = "assets"
 const DirIndexFile = "index.html"
 

+ 3 - 1
database/schema.sql

@@ -4,6 +4,8 @@ CREATE TABLE `users` (
 	`last_name` varchar(64) NOT NULL DEFAULT '' COMMENT 'User last name',
 	`email` varchar(64) NOT NULL COMMENT 'User email',
 	`password` varchar(32) NOT NULL COMMENT 'User password (MD5)',
+	`admin` int(1) NOT NULL COMMENT 'Is admin user or not',
+	`active` int(1) NOT NULL COMMENT 'Is active user or not',
 	PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
@@ -17,6 +19,6 @@ CREATE TABLE `pages` (
 	`meta_keywords` varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta keywords',
 	`meta_description` varchar(510) NOT NULL DEFAULT '' COMMENT 'Page meta description',
 	`datetime` datetime NOT NULL COMMENT 'Creation date/time',
-	`status` enum('draft','public','trash') NOT NULL COMMENT 'Page status',
+	`active` int(1) NOT NULL COMMENT 'Is active page or not',
 	PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

+ 7 - 0
engine/builder/data_form.go

@@ -12,6 +12,7 @@ const (
 	DFKEmail
 	DFKPassword
 	DFKTextArea
+	DFKCheckBox
 	DFKSubmit
 	DFKMessage
 )
@@ -65,6 +66,12 @@ func DataForm(wrap *wrapper.Wrapper, data []DataFormField) string {
 					result += `<input class="form-control" type="password" id="lbl_` + field.Name + `" name="` + field.Name + `" value="` + html.EscapeString(field.Value) + `" placeholder="` + field.Placeholder + `" autocomplete="off"` + required + `>`
 				} else if field.Kind == DFKTextArea {
 					result += `<textarea class="form-control" id="lbl_` + field.Name + `" name="` + field.Name + `" placeholder="` + field.Placeholder + `" autocomplete="off"` + required + `>` + html.EscapeString(field.Value) + `</textarea>`
+				} else if field.Kind == DFKCheckBox {
+					checked := ""
+					if field.Value != "0" {
+						checked = " checked"
+					}
+					result += `<input class="form-control" type="checkbox" id="lbl_` + field.Name + `" name="` + field.Name + `" value="1"` + `" autocomplete="off"` + required + checked + `>`
 				}
 				result += `</div>`
 				if field.Hint != "" {

+ 6 - 0
engine/engine.go

@@ -105,6 +105,12 @@ func (this *Engine) Process() bool {
 		return true
 	}
 
+	// Only active admins can use backend
+	if !(this.Wrap.User.A_admin == 1 && this.Wrap.User.A_active == 1) {
+		utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpLogin, nil)
+		return true
+	}
+
 	// Render backend
 	return this.Mods.XXXBackEnd(this.Wrap)
 }

+ 5 - 1
engine/wrapper/wrapper.go

@@ -109,7 +109,9 @@ func (this *Wrapper) LoadSessionUser() bool {
 			first_name,
 			last_name,
 			email,
-			password
+			password,
+			admin,
+			active
 		FROM
 			users
 		WHERE
@@ -122,6 +124,8 @@ func (this *Wrapper) LoadSessionUser() bool {
 		&user.A_last_name,
 		&user.A_email,
 		&user.A_password,
+		&user.A_admin,
+		&user.A_active,
 	)
 	if err != nil {
 		return false

+ 10 - 4
modules/module_index.go

@@ -70,7 +70,7 @@ func (this *Modules) RegisterModule_Index() *Module {
 					NameInTable: "Date / Time",
 				},
 				{
-					DBField:     "status",
+					DBField:     "active",
 					NameInTable: "Active",
 				},
 			}, func(values *[]string) string {
@@ -144,6 +144,8 @@ func (this *Modules) RegisterAction_MysqlSetup() *Action {
 				last_name VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User last name',
 				email VARCHAR(64) NOT NULL COMMENT 'User email',
 				password VARCHAR(32) NOT NULL COMMENT 'User password (MD5)',
+				admin int(1) NOT NULL COMMENT 'Is admin user or not',
+				active int(1) NOT NULL COMMENT 'Is active user or not',
 				PRIMARY KEY (id)
 			) ENGINE = InnoDB;`,
 			pf_name))
@@ -162,7 +164,7 @@ func (this *Modules) RegisterAction_MysqlSetup() *Action {
 				meta_keywords varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta keywords',
 				meta_description varchar(510) NOT NULL DEFAULT '' COMMENT 'Page meta description',
 				datetime datetime NOT NULL COMMENT 'Creation date/time',
-				status enum('draft','public','trash') NOT NULL COMMENT 'Page status',
+				active int(1) NOT NULL COMMENT 'Is active page or not',
 				PRIMARY KEY (id)
 			) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
 			pf_name))
@@ -213,7 +215,9 @@ func (this *Modules) RegisterAction_CpFirstUser() *Action {
 				first_name = ?,
 				last_name = ?,
 				email = ?,
-				password = MD5(?)
+				password = MD5(?),
+				admin = 1,
+				active = 1
 			;`,
 			pf_first_name,
 			pf_last_name,
@@ -266,7 +270,9 @@ func (this *Modules) RegisterAction_CpUserLogin() *Action {
 				users
 			WHERE
 				email = ? and
-				password = MD5(?)
+				password = MD5(?) and
+				admin = 1 and
+				active = 1
 			LIMIT 1;`,
 			pf_email,
 			pf_password,

+ 45 - 6
modules/module_users.go

@@ -55,6 +55,14 @@ func (this *Modules) RegisterModule_Users() *Module {
 				{
 					DBField: "last_name",
 				},
+				{
+					DBField:     "admin",
+					NameInTable: "Admin",
+				},
+				{
+					DBField:     "active",
+					NameInTable: "Active",
+				},
 			}, func(values *[]string) string {
 				return `<a class="ico" href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[0] + `/">` +
 					assets.SysSvgIconEdit + `</a>` +
@@ -76,6 +84,8 @@ func (this *Modules) RegisterModule_Users() *Module {
 				A_first_name: "",
 				A_last_name:  "",
 				A_email:      "",
+				A_admin:      0,
+				A_active:     0,
 			}
 
 			if wrap.CurrSubModule == "modify" {
@@ -90,7 +100,9 @@ func (this *Modules) RegisterModule_Users() *Module {
 						id,
 						first_name,
 						last_name,
-						email
+						email,
+						admin,
+						active
 					FROM
 						users
 					WHERE
@@ -102,12 +114,21 @@ func (this *Modules) RegisterModule_Users() *Module {
 					&data.A_first_name,
 					&data.A_last_name,
 					&data.A_email,
+					&data.A_admin,
+					&data.A_active,
 				)
 				if err != nil {
 					return "", "", ""
 				}
 			}
 
+			pass_req := true
+			pass_hint := ""
+			if wrap.CurrSubModule == "modify" {
+				pass_req = false
+				pass_hint = "Leave the field blank to not change the password"
+			}
+
 			content += builder.DataForm(wrap, []builder.DataFormField{
 				{
 					Kind:  builder.DFKHidden,
@@ -139,10 +160,23 @@ func (this *Modules) RegisterModule_Users() *Module {
 					Required: true,
 				},
 				{
-					Kind:    builder.DFKPassword,
-					Caption: "Password",
-					Name:    "password",
-					Hint:    "Leave the field blank to not change the password",
+					Kind:     builder.DFKPassword,
+					Caption:  "Password",
+					Name:     "password",
+					Required: pass_req,
+					Hint:     pass_hint,
+				},
+				{
+					Kind:    builder.DFKCheckBox,
+					Caption: "Admin",
+					Name:    "admin",
+					Value:   utils.IntToStr(data.A_admin),
+				},
+				{
+					Kind:    builder.DFKCheckBox,
+					Caption: "Active",
+					Name:    "active",
+					Value:   utils.IntToStr(data.A_active),
 				},
 				{
 					Kind: builder.DFKMessage,
@@ -153,7 +187,12 @@ func (this *Modules) RegisterModule_Users() *Module {
 					Target: "add-edit-button",
 				},
 			})
-			sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
+
+			if wrap.CurrSubModule == "add" {
+				sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
+			} else {
+				sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
+			}
 		}
 		return this.getSidebarModules(wrap), content, sidebar
 	})

+ 1 - 1
utils/mysql_struct_pages.go

@@ -10,5 +10,5 @@ type MySql_page struct {
 	A_meta_keywords    string
 	A_meta_description string
 	A_datetime         int
-	A_status           int
+	A_active           int
 }

+ 2 - 0
utils/mysql_struct_users.go

@@ -6,4 +6,6 @@ type MySql_user struct {
 	A_last_name  string
 	A_email      string
 	A_password   string
+	A_admin      int
+	A_active     int
 }

Some files were not shown because too many files changed in this diff