-: Hi, guys, and welcome back.
In this video, we're going to learn
about decorators with parameters.
Now, this is slightly different than the last video
where we looked at decorating functions with parameters,
now we're going to add parameters
to the decorators themselves.
Let's get started.
Here, we have our make_secure decorator.
And now, we are looking at two different functions.
One get_admin_password that is decorated,
and get_dashboard_password, which is also decorated.
But now, I want the get_admin_password
function to only be accessible by admins
and the get_dashboard_password to only
be accessible by users.
So if you're a guest, you will get access to neither,
but if you're a user, you'll get access to this one.
And if you're an admin, you'll get access to this one.
So how do we do this?
Well, we have a make_secure decorator here,
and notice that when this gets called,
what happens is the make_secure decorator
gets applied to get_admin_password.
But there are no brackets after this make_secure.
Now, what we're going to do is we're going to create
a function that will return the decorator,
but it will allow us to check for the access level.
Now, here's that's gonna go.
It's a little bit complicated.
We're going to create another decorator,
and this one is going to take in an access_level.
The rest of all of this is going to go inside that.
And this one here is going to return make_secure.
So we have added a third level of function,
and I know this is quite confusing,
but now what we've got is not really a decorator.
Now, I'm going to actually change the name
of these functions because the outer function,
it's not really a decorator anymore.
This one here is a decorator now.
This one is a factory, essentially,
a function that is used to create a decorator.
But what we have is a function call,
that we give it an access_level,
and then it creates a decorator and returns it.
So now we have to put the brackets in here.
And the order is very important.
The function call happens first,
and then the result is applied as a decorator.
So the function call happens first.
This will run in its entirety.
We will define the decorator function,
and we will return it.
Oh, sorry, this should be decorator there,
because we wanna return that function.
So this will run and return the decorator,
and that is the result of calling make_secure,
and then we will apply that decorator into this function.
At the end of the day, the result is the same,
but now we can pass in something like admin in there,
and something like guest in there.
Notice that because this factory does take in a argument,
you need to put the argument and the brackets in there.
The benefit of that is now inside here,
we can check that access level.
So we can say that if the user's access level
is equal to the access level that we want to secure for,
then we have access to the function.
Otherwise, we can say that this user
has no access level permissions, or for that.
So there are no access level permissions for that user.
Now if we want to run these functions, we can do.
Here I've got the get_admin_password
and get_dashboard_password.
We should get no access for either of those.
And then I'm going to change the user to an admin
and run them again, and now we should allow this one,
but not that one.
Let me save that and run it,
and you'll see that you get that problem exactly.
Indeed, we actually have access to the user dashboard there.
We wanna change that, make sure that's for users only
since that was the original intention.
Let's run that again.
And you get no admin permissions for Jose,
no user permissions for Jose.
But Anna does have admin permissions,
but no user permissions.
something that you may wanna look into
to improve this further is, instead of using strings
for your access levels, you can use integers.
So guest can be zero, user can be one,
and admin can be two.
And then, instead of checking a quality here,
you can actually do greater than.
And that'll let admins access the user passwords
as well, if you want that.
That's it for this video.
I wanted to show you how to create decorators
that have parameters.
And I know that they are a bit confusing.
There are three levels of functions,
which make these the most complicated Python code
that you'll see in most Python courses,
and certainly throughout your programming, as well.
But hope you've learned something.
Hope you've enjoyed it.
And I'll see you in the next video.