Jump to content




User permissions

help

  • You cannot reply to this topic
8 replies to this topic

#1 TechMasterGeneral

  • Members
  • 149 posts
  • LocationUnited States

Posted 14 February 2014 - 09:24 AM

So.... I have another question to ask about tables...
I want to make a user permissions system to control adding and deleting users... I have the adding and deleting part down... but i need to know if i can add permissions to the same key/value pair as the username and password or if i have to make another table... I also need help checking the permissions... here is the code i have so far...
Spoiler

You can comment on certain lines with this website:
http://chopapp.com/#hzbgpo28


Nevermind... doesn't work for some reason

Edited by LuaCrawler, 14 February 2014 - 09:27 AM.


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 14 February 2014 - 11:28 AM

Say you take these tables:

local users = {
        ["Admin"] = "password",
        ["JoeUser"] = "anotherPassword"
}
local userPerm = {
        ["Admin"] = "admin",
        ["JoeUser"] = "user"
}

... and redefine them like this:

local users = {
        ["Admin"] = {["password"] = "password", ["permissions"] = "admin"},
        ["JoeUser"] = {["password"] = "anotherPassword", ["permissions"] = "user"}
}

Every index in the "users" table now points to another table.

This allows you to eg get the password for "Admin" by checking the value of:

users["Admin"].password

So say you had a username stored in the variable "curUser", and you wanted to know if that user existed, you'd just do:

if users[curUser] then
        -- User exists.
else
        -- User does not exist.
end

And if you wanted to compare their permissions, you'd do something like:

if users[curUser].permissions == "admin" then ...

Edit: Note that you can't index into a table that doesn't exist. If a username isn't in your users table, then attempting to check their eg password will cause your script to error out. Check that the user's in the table first, then check what their password/permissions/whatever are.

Edited by Bomb Bloke, 14 February 2014 - 11:31 AM.


#3 TechMasterGeneral

  • Members
  • 149 posts
  • LocationUnited States

Posted 14 February 2014 - 06:21 PM

View PostBomb Bloke, on 14 February 2014 - 11:28 AM, said:

Say you take these tables:

local users = {
		["Admin"] = "password",
		["JoeUser"] = "anotherPassword"
}
local userPerm = {
		["Admin"] = "admin",
		["JoeUser"] = "user"
}

... and redefine them like this:

local users = {
		["Admin"] = {["password"] = "password", ["permissions"] = "admin"},
		["JoeUser"] = {["password"] = "anotherPassword", ["permissions"] = "user"}
}

Every index in the "users" table now points to another table.

This allows you to eg get the password for "Admin" by checking the value of:

users["Admin"].password

So say you had a username stored in the variable "curUser", and you wanted to know if that user existed, you'd just do:

if users[curUser] then
		-- User exists.
else
		-- User does not exist.
end

And if you wanted to compare their permissions, you'd do something like:

if users[curUser].permissions == "admin" then ...

Edit: Note that you can't index into a table that doesn't exist. If a username isn't in your users table, then attempting to check their eg password will cause your script to error out. Check that the user's in the table first, then check what their password/permissions/whatever are.

So then i wouldn't need the
for k,v in pairs(users) do
i would just do
if users[curUser] then
--Run code here
?

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 14 February 2014 - 06:44 PM

Exactly.

#5 TechMasterGeneral

  • Members
  • 149 posts
  • LocationUnited States

Posted 14 February 2014 - 08:20 PM

So then to add a user with their permissions to the table i would need to do:
users[usr] = pass, perm
?

#6 TechMasterGeneral

  • Members
  • 149 posts
  • LocationUnited States

Posted 14 February 2014 - 08:38 PM

What would be the best way to keep from starting a loop all over again... cus i want if a user gets a password wrong i don't want it to log out... also... would it be better if i made all the sections of if code to be functions... i'm thinking it may be better to if this param is true then do this function... if not then do this function..

#7 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 15 February 2014 - 01:28 AM

View PostLuaCrawler, on 14 February 2014 - 08:20 PM, said:

So then to add a user with their permissions to the table i would need to do:
users[usr] = pass, perm
?
No, that's not right - the idea is that you store a table against users[user], so you end up with a collection of tables in a table. In your code block there you've just put a list of variables in it (only the first of which would actually be captured).

You could do it like this:

users[usr] = {["password"] = pass, ["permissions"] = perm}

Or like this:

users[usr] = {}
users[usr].password = pass
users[usr].permissions = perm

Or a variety of other ways, which boil down to rephrasings of the same thing - "put a table in this table, fill it with these values".

View PostLuaCrawler, on 14 February 2014 - 08:38 PM, said:

What would be the best way to keep from starting a loop all over again... cus i want if a user gets a password wrong i don't want it to log out...
Don't quite getcha here. If a user fails to log in, then surely they'd expect to be logged out? What are you intending to happen instead?

View PostLuaCrawler, on 14 February 2014 - 08:38 PM, said:

also... would it be better if i made all the sections of if code to be functions... i'm thinking it may be better to if this param is true then do this function... if not then do this function..
Go with whatever you find easier to read and maintain.

Personally I dislike using functions unless doing so will actually decrease the length of my script - for example, if I want to run the same code twice under different circumstances, it'll generally get dumped into a function. A classic example would be if you want to write text in a special way (eg centered) on a regular basis.

On some occasions, functions can be handy for escaping loops - say you're nested three levels deep, and you suddenly wish to break out of them all. It can be done, but it's not as simple as inserting three break statements (and ComputerCraft's Lua lacks a "goto")! But if those loops are isolated away inside a function, then you need only return from that in order to escape them all.

#8 TechMasterGeneral

  • Members
  • 149 posts
  • LocationUnited States

Posted 15 February 2014 - 09:47 AM

View PostBomb Bloke, on 15 February 2014 - 01:28 AM, said:

View PostLuaCrawler, on 14 February 2014 - 08:20 PM, said:

So then to add a user with their permissions to the table i would need to do:
users[usr] = pass, perm
?
No, that's not right - the idea is that you store a table against users[user], so you end up with a collection of tables in a table. In your code block there you've just put a list of variables in it (only the first of which would actually be captured).

You could do it like this:

users[usr] = {["password"] = pass, ["permissions"] = perm}

Or like this:

users[usr] = {}
users[usr].password = pass
users[usr].permissions = perm

Or a variety of other ways, which boil down to rephrasings of the same thing - "put a table in this table, fill it with these values".

View PostLuaCrawler, on 14 February 2014 - 08:38 PM, said:

What would be the best way to keep from starting a loop all over again... cus i want if a user gets a password wrong i don't want it to log out...
Don't quite getcha here. If a user fails to log in, then surely they'd expect to be logged out? What are you intending to happen instead?

View PostLuaCrawler, on 14 February 2014 - 08:38 PM, said:

also... would it be better if i made all the sections of if code to be functions... i'm thinking it may be better to if this param is true then do this function... if not then do this function..
Go with whatever you find easier to read and maintain.

Personally I dislike using functions unless doing so will actually decrease the length of my script - for example, if I want to run the same code twice under different circumstances, it'll generally get dumped into a function. A classic example would be if you want to write text in a special way (eg centered) on a regular basis.

On some occasions, functions can be handy for escaping loops - say you're nested three levels deep, and you suddenly wish to break out of them all. It can be done, but it's not as simple as inserting three break statements (and ComputerCraft's Lua lacks a "goto")! But if those loops are isolated away inside a function, then you need only return from that in order to escape them all.
Kk... I see..
I wanted it so that say... when asked for the username Admin accidentally puts in admin... i don't want it to go and logout i just want it to go back to the beginning and ask for the username again...
I just wasn't sure about making all that stuff functions... i though it may be more efficient... but i think i'll leave it until i get the code stable then reconsider

#9 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 15 February 2014 - 07:44 PM

Ah, I see - the script asks for a username more then once, and you're not talking about the first instance.

The answer is more "while"/"repeat" loops, which are generally the way to go whenever you want something looped until a given condition is met.

Here's an example of a quick login process which will repeat its requests for a valid username/password combo until it gets one:

local users = {
                ["Admin"] = {["password"] = "password", ["permissions"] = "admin"},
                ["Mod"] = {["password"] = "anotherPassword", ["permissions"] = "moderator"},
                ["JoeUser"] = {["password"] = "yetAnotherPassword", ["permissions"] = "user"}
}

local curUser,curPass

while (not users[curUser]) or users[curUser].password ~= curPass do
	print("Enter username:")
	curUser = read()

	print("Enter password:")
	curPass = read()

	if (not users[curUser]) or users[curUser].password ~= curPass then
		print("Error: Invalid username/password combination. Please try again.")
		print("")
	end
end

-- User has logged in, carry on down here...

You could add in extra conditions or loops to make it specifically wait for users with certain permission levels, eg:

local users = {
                ["Admin"] = {["password"] = "password", ["permissions"] = "admin"},
                ["Mod"] = {["password"] = "anotherPassword", ["permissions"] = "moderator"},
                ["JoeUser"] = {["password"] = "yetAnotherPassword", ["permissions"] = "user"}
}

local curUser,curPass

while (not users[curUser]) or users[curUser].password ~= curPass or users[curUser].permissions ~= "admin" do
	print("Enter username:")
	curUser = read()

	print("Enter password:")
	curPass = read()

	if (not users[curUser]) or users[curUser].password ~= curPass then
		print("Error: Invalid username/password combination. Please try again.")
		print()
	elseif users[curUser].permissions ~= "admin" then
		print("Login valid, but lacks sufficient privileges. Please try another account.")
		print()
	end
end

-- User has logged in, carry on down here...






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users