Merge pull request 'feat: added authorization' (#5) from develop into production

Reviewed-on: #5
pull/10/head
Евгений Сугоняко 2024-05-11 12:42:16 +00:00
commit 15fdb20c2b
4 changed files with 54 additions and 14 deletions

View File

@ -1,4 +1 @@
{
"login": "administrator",
"password": "9a3d1f5ce0bbbdf38ca26740bdc5aa55"
}
{"login":"administrator","name":"Admin","password":"9a3d1f5ce0bbbdf38ca26740bdc5aa55","token":"349f8bbac7070468faf7f13db08945db","userType":"Administrator"}

View File

@ -0,0 +1,7 @@
{
"email": "test_user@gmail.com",
"password": "9a3d1f5ce0bbbdf38ca26740bdc5aa55",
"token": "",
"userType": "User",
"name": "test_user"
}

View File

@ -2,17 +2,45 @@ class_name ORM extends Object
var MODEL : Dictionary
var db_path : String
#--------------------------------------------------------------------------------------------------#
# Initialization
func _init(_db_path = "res://data/DataBase"):
var baseDir := DirAccess.open(_db_path)
db_path = "res://data/DataBase"
var baseDir := DirAccess.open(db_path)
for model in baseDir.get_directories():
MODEL[model] = {route = _db_path.path_join(model), instancies = []}
var modelDir := DirAccess.open(_db_path.path_join(model))
MODEL[model] = {route = db_path.path_join(model), instancies = {}}
var modelPath := db_path.path_join(model)
var modelDir := DirAccess.open(modelPath)
for instance in modelDir.get_files():
MODEL[model]["instancies"].append(instance.trim_suffix('.json'))
MODEL[model]["instancies"][instance.trim_suffix('.json')] = modelPath.path_join(instance)
#--------------------------------------------------------------------------------------------------#
# Methods
# Get
func findOne(_model, _instance):
if !MODEL.has(_model):
return null
if !MODEL[_model].instancies.has(_instance):
return null
var file = FileAccess.open(MODEL[_model].instancies[_instance], FileAccess.READ)
var response = JSON.parse_string(file.get_as_text())
file.close()
return response
#--------------------------------------------------------------------------------------------------#
# Methods
# Set
func update(_model, _instance, _data):
if !MODEL.has(_model):
return null
if !MODEL[_model].instancies.has(_instance):
return null
var file = FileAccess.open(MODEL[_model].instancies[_instance], FileAccess.WRITE)
file.store_string(JSON.stringify(_data))
file.close()
return true

View File

@ -121,13 +121,21 @@ func __on_client_disconnected(_id):
# RPC Server
@rpc("any_peer", "reliable", "call_local", 1)
func authorization(_login, _password):
var client_id = multiplayer.get_remote_sender_id()
var player = _get_player(client_id)
var token : String = str(client_id) + ".token"
var db = ORM.new()
var client_id : int = multiplayer.get_remote_sender_id()
var player : Node2D = _get_player(client_id)
var token : String = str(client_id).md5_text()
var isAuthorized : bool = false
if _login == '123':
isAuthorized = true
var rpc_response = player.rpc_id(client_id, 'authorizated', token, isAuthorized)
var user = db.findOne("User", _login)
if user:
if _password == user.password:
token = (token + str(user.password)).md5_text()
user.token = token
player.token = token
player.playerName = user.name
if db.update("User", _login, user):
isAuthorized = true
var rpc_response = player.rpc_id(client_id, 'authorizated', player.playerName, token, isAuthorized)
if rpc_response != Error.OK:
FileManager.add_to_log("ERROR | Status: %s | Message: CLIENT NOT AUTHORIZATED id: %s - Error: %s" % [STATUS.ERROR, client_id, error_string(rpc_response)])
else: