瀏覽代碼

Basics of working MvC with multiple users and simple scripts

Yentl Van Tendeloo 8 年之前
父節點
當前提交
84301964ff
共有 6 個文件被更改,包括 78 次插入31 次删除
  1. 56 18
      core/core_algorithm.alc
  2. 1 1
      core/core_formalism.mvc
  3. 2 1
      kernel/modelverse_kernel/primitives.py
  4. 8 9
      scripts/compile.py
  5. 9 1
      scripts/execute_model.py
  6. 2 1
      scripts/prompt.py

+ 56 - 18
core/core_algorithm.alc

@@ -32,6 +32,18 @@ Void function main():
 	core = instantiate_model(import_node(core_location))
 	export_node(core_model_location, core)
 
+	// Switch all new users to the user_function
+	// This accesses the bootstrap level, so do not change this unless you know what you are doing
+	Element root
+	Element prev
+	root = read_root()
+	root = root["__hierarchy"]["__IP"]
+	while (value_neq(root, !call)):
+		prev = root
+		root = root["next"]
+	dict_delete(prev, "next")
+	dict_add(prev, "next", user_function["body"])
+
 	// Create admin group
 	admin_group = instantiate_node(core, "Group", "")
 	instantiate_attribute(core, admin_group, "name", "admin")
@@ -46,6 +58,23 @@ Void function main():
 	instantiate_attribute(core, admin_user, "name", input())
 	instantiate_attribute(core, admin_user, "admin", True)
 
+	Boolean ct
+	String password
+
+	ct = True
+	while (ct):
+		output("Desired password for admin user?")
+		password = hash(input())
+
+		output("Please repeat the password")
+		if (password == hash(input())):
+			output("Passwords match!")
+			output("User created")
+			instantiate_attribute(core, admin_user, "password", password)
+			ct = False
+		else:
+			output("Not the same password, please try again!")
+
 	// Create link between admin user and group
 	instantiate_link(core, "ownedBy", "", admin_group, admin_user)
 	instantiate_link(core, "belongsTo", "", admin_user, admin_group)
@@ -83,18 +112,6 @@ Void function main():
 	instantiate_link(core, "group", "", core_model, admin_group)
 	instantiate_link(core, "owner", "", core_model, admin_user)
 
-	// Switch all new users to the user_function
-	// This accesses the bootstrap level, so do not change this unless you know what you are doing
-	Element root
-	Element prev
-	root = read_root()
-	root = root["__hierarchy"]["__IP"]
-	while (value_neq(root, !call)):
-		prev = root
-		root = root["next"]
-	dict_delete(prev, "next")
-	dict_add(prev, "next", user_function["body"])
-
 	// Call this for ourselves as well
 	user_function_skip_init(admin_user)
 
@@ -173,17 +190,19 @@ Boolean function allow_group_modify(user_id : String, group_id : String):
 
 Boolean function check_login(user_id : String):
 	String password
+	String stored_password
+
+	stored_password = read_attribute(core, user_id, "password")
 
 	output("Password for existing user?")
-	password = input()
+	password = hash(input())
 
-	return True!
+	return password == stored_password!
 
 Element function user_function():
 	String username
 	String user_id
-
-	output("Log on as which user?")
+	String password
 
 	// Load in all global variables, as this code is hotloaded!
 	Element root
@@ -201,6 +220,7 @@ Element function user_function():
 	exec(root["core/core_algorithm.alc"]["initializers"])
 	core = import_node("models/core")
 
+	output("Log on as which user?")
 	username = input()
 
 	user_id = get_user_id(username)
@@ -211,11 +231,29 @@ Element function user_function():
 		instantiate_attribute(core, user_id, "name", username)
 		instantiate_attribute(core, user_id, "admin", False)
 
+		Boolean ct
+		ct = True
+
+		while (ct):
+			output("This is a new user: please give password!")
+			password = hash(input())
+
+			output("Please repeat the password")
+			if (password == hash(input())):
+				output("Passwords match!")
+				output("User created")
+				instantiate_attribute(core, user_id, "password", password)
+				ct = False
+			else:
+				output("Not the same password, please try again!")
+
 		// Now call with user created
 		user_function_skip_init(user_id)
 	else:
-		if (check_login(user_id)):
-			user_function_skip_init(user_id)
+		while (bool_not(check_login(user_id))):
+			output("Wrong password! Try again")
+		
+		user_function_skip_init(user_id)
 
 	// User destroyed already, so just stop execution
 	// TODO return a fresh node as otherwise the compiler doesn't take this

+ 1 - 1
core/core_formalism.mvc

@@ -42,8 +42,8 @@ SimpleClassDiagrams CoreFormalism {
 
     Class User {
         name : String
+        password : String
         admin : Boolean
-        nr_logins : Natural
     }
 
     Class Group {

+ 2 - 1
kernel/modelverse_kernel/primitives.py

@@ -570,5 +570,6 @@ def time(**remainder):
 def hash(a, **remainder):
     a_value, = yield [("RV", [a])]
     import hashlib
-    b = hashlib.sha512(a_value).hexdigest()
+    b_value = hashlib.sha512(a_value).hexdigest()
+    b, = yield [("CNV", [b_value])]
     raise PrimitiveFinished(b)

+ 8 - 9
scripts/compile.py

@@ -13,15 +13,14 @@ def do_compile(address, filename, username, modulename, mode, optionals=['--debu
         else:
             # Load AL grammar
             grammar = "grammars/actionlanguage.g"
-    try:
-        urllib2.urlopen(urllib2.Request(address, 'op=set_input&username=user_manager&value="%s"' % username)).read()
-        subprocess.check_call([sys.executable, "hutn_compiler/compiler.py", filename, grammar, mode, username, modulename, filename, address] + optionals, cwd="interface/HUTN")
-    except urllib2.URLError:
-        return 2
-    except:
-        urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '-1', "username": username}))).read()
-        return 1
-    urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '-1', "username": username}))).read()
+    while 1:
+        try:
+            # Create new user
+            urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % username, "username": "user_manager"}))).read()
+            subprocess.check_call([sys.executable, "hutn_compiler/compiler.py", filename, grammar, mode, username, modulename, filename, address] + optionals, cwd="interface/HUTN")
+            break
+        except:
+            time.sleep(0.01)
     return 0
 
 if __name__ == "__main__":

+ 9 - 1
scripts/execute_model.py

@@ -2,6 +2,7 @@ import random
 import sys
 import multiprocessing
 import glob
+import time
 
 from compile import do_compile
 from link_and_load import link_and_load
@@ -28,7 +29,14 @@ def initialize_SCD():
     import urllib
     import json
     username = random.random()
-    urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % username, "username": "user_manager"}))).read()
+
+    while 1:
+        try:
+            # Create new user
+            urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % username, "username": "user_manager"}))).read()
+            break
+        except:
+            time.sleep(0.01)
 
     data = []
     data.append(4)

+ 2 - 1
scripts/prompt.py

@@ -35,7 +35,8 @@ except IndexError:
 try:
     username = sys.argv[2]
 except IndexError:
-    username = "test"
+    import random
+    username = str(random.random())
 
 # If user doesn't exist yet, we create it
 urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % username, "username": "user_manager"}))).read()