Regardless of the system, user rights are critical. So when I was registering users, I gave them two permissions. One is the normal user's permission, one is the administrator privilege. The value of the average user in the database is user, and the admin value is admin.
Because I want to design a system that has only one administrator. So here I'm not judging by the character. It is judged by his user name. The administrator's user name is unique, and that is admin.
Gossip less, first put the registered user's code up
Logical side:
@app. Route ("/adduser", methods=[' GET ', ' POST ') def adduser (): if request.method == "GET": username=session.get ("name") return render_template ("register.html", Username=username) #前端post请求, the logical end gets the value of the entire form through Request.Form if request.method == "POST": userlist=dict (K,v[0]) for k,v in dict (request.form). Items ()) userlist[' Password ']=hashlib.md5 (userlist[' password ']+salt). Hexdigest () userlist['Re_password ']=hashlib.md5 (userlist[' Re_password ']+salt). Hexdigest () if userlist["Name"] in [ n.values () [ 0] for n in get_userlist (["name"]) ]: errmsg = "Username is exist" return json.dumps ({' Code ': ' 1 ', ' ErrMsg ': errmsg}) if not userlist["name"] or not userlist["Password"]: errmsg = " Username and password is not empty " return Json.dumps ({' Code ': ' 1 ', ' errmsg ': errmsg}) if userlist["Password"] != userlist["Re_password"]: errmsg= "Password is error" return Json.dumps ({' Code ': ' 1 ', ' errmsg ': errmsg}) fields = ["Name", "NAME_CN", "Password", "mobile", "email", "role", "status" ] values = [ '%s '% userlist[x] for x in fields] userdict = dict ([(K,values[i]) for i,k in Enumerate (Fields)]) add_user (userdict) return json.dumps ({' Code ': ' 0 ', ' result ': ' register sucess '})
Data-side code:
def add_user (userlist): sql= "INSERT into users (%s) values ('%s ')"% (",". Join (Userlist.keys ()), "', '". Join (Userlist.val UEs ()) curs.execute (SQL) Conn.commit ()
Front-End Code:
... Ellipsis <div class= "Form-group" > <label for= "Password" class= "Col-sm-2 control-label" > Password <span class= "red-fonts" ></span></label> <div class= "Col-sm-8" > <input id= "PAssWOrd " name=" password " placeholder=" password " type=" password " class=" Form-control "> </div> </div> <div class= "Form-group" > <label for= "Password" class= "Col-sm-2 control-label" > Confirm password </ Label> <div class= "Col-sm-8" > <input id= "Re_password" name= "Re_password" placeholder= "Confirm password Again" type= "password" class= " Form-control "> </div > </div> <div class= "Form-group" > <label for= "Role" class= "Col-sm-2 control-label" > Roles </label> <div class= "Col-sm-8" > < Select name= "Role" > < Option value= "admin" class= "Form-control" > Admin </option> <option value= "User" class= "Form-control" > General users </option> </select> </div> </div> <div class= "Form-group" > <label for= "status" class= "col-sm-2 Control-label "> Status </label> <div class= "Col-sm-8" > <select name= "Status" > <option value= "0" class= "Form-control" > Activation </option > <option value= "1" class= "Form-control" > Lock </option> </select> </div> </div>, ..... Omitted
Because my front end is applied from somewhere else. I'm using inheritance here.
Judge the user's rights in the base.html template.
<ul class= "nav nav-second-level collapse" > <li class= "group" ><a href= "/userinfo" > Personal Center </A>&L t;/li> {%if username== ' admin '%} <li class= "user" ><a href= "/userlist" > User list </a></li> ; {%endif%} </ul>
The username here is transmitted from the logical side to the front end. Careful students to see the next to know
<li id= "Jasset" ><a><i class= "Fa fa-inbox" ></i> <span class= "Nav-label" > Asset Management </span><span class= "Fa arrow" ></span></a> {%if username== ' admin '%} <ul class= "Nav nav-second-level collapse" > <li class= "group" ><a href= "/IDC" > Computer room Management </a></ li> <li class= "Asset" > <a href= "/ Cabinet "> Cabinet Management </a></li> <li class=" IDC " > <a href= "/server" > Server Management </a></li> </ul > {%endif%}</li> <!-- Asset Management -->
to here. User rights are done.
The average user can only see his own personal information.
Admin users can see all the information
This article from "Do not abandon!" Do not give up "blog, declined reprint!"
Python Learning Tenth the CMDB user Rights management