From 0298763bca103422b31041a6daed3d4eb564d8e1 Mon Sep 17 00:00:00 2001 From: Amigo <49749100+aamigo@users.noreply.github.com> Date: Wed, 12 Jun 2019 14:03:25 +0200 Subject: [PATCH] Updated 024 How to integrate the Create User Helper Method in your Components (markdown) --- ...te-User-Helper-Method-in-your-Components.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/024-How-to-integrate-the-Create-User-Helper-Method-in-your-Components.md b/024-How-to-integrate-the-Create-User-Helper-Method-in-your-Components.md index 67c9d1b..32d4bc0 100644 --- a/024-How-to-integrate-the-Create-User-Helper-Method-in-your-Components.md +++ b/024-How-to-integrate-the-Create-User-Helper-Method-in-your-Components.md @@ -21,6 +21,18 @@ Let me show you another one. Obviously you realise I'm using a lot of JavaScript So let me illustrate some of or let me show you some of the code that I used to do that. [00:09:26](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h09m26s) So we are back in the experts view. As I've done before I previously, in other tutorials explained how to make use of the Ajax concept within Component Builder. Now for those of you that like to understand what's going on let me give you a quick runover at the code level. Within every component that makes use of Ajax, a model, and a controller is created. The controller is named Ajax.json.php and the model is named [00:10:04](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h10m04s) Ajax. Let me open the model, let me also open the controller. As you can see the controller basic fires off the register tasks within the construct method. And then it adds the tasks into a switch, and tries to run the task. Also in [00:10:31](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h10m31s) the model it gets the model Ajax, and then runs the method that basically corresponds to the task. And so that is how it runs. -It uses the Joomla's default filtering method in the way that it gets values that are being posted. And then value dates that those values actually are there. Then sends the values over to the model. [00:11:05](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h11m05s) Now that's looking at it from a code side. If you look at the model it simply shows you various methods. Some of them are protected, and some of them are public. Those(getcreateuserfields) are usually the ones that gets called from the controller once the Ajax gets the verification level and it's verified. Here is the createuser class. [00:11:31](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h11m31s) We first check whether this user, (now this is custom script I mean this I wrote custom, component build does not write this) so basically when you go back to the user interface, you will see that if I scroll down here, create user fields, then there's create user. You see that I wrote this right here, and that basically gets placed into the model. [00:12:07](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h12m07s) So that's the Ajax method. In that Ajax input I annotate the specific tasks, the task name, and specific variables, and again the method name. -??? -You could have different method names from task names. What should be validated, whether it's integer, float or whatever. And if there's no value, what is the value by default. So this is where you set up there basically the controllers information. [00:12:39](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h12m39s) And then here your customisely write your script. So basically create user gets data and seems from what view is it, then validates with this current login user has the permission to the experts area. Then it converts the data because it's a json set of datasets that I'm sending across. Checks whether it's an array, does some other checks. So you can see I'm still here I'm using that [00:13:17](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h13m17s) Global helper Class and methods. Here are number of custom methods obviously that is available in that class. Go through it. We just add it in there as freebies and you want to always see the need of them but I'm using them ofte.n And even if you have with Improvements on those methods please make a pull request and we'll make those changes Then you over here once we've Become very sure that we've got enough data on the right data We basically get that same method and we need to create user [00:13:55](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h13m55s) Get result And if it's not the correct result we deal with it accordingly And if it is the correct result we create Add the user to the user group That's something that the user does not that you create user does not do So we Then have a user group setup [00:14:17](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h14m17s) And Basically in the component And we get that user group Set into the correct User Group This is a return message If it's an error or whether it's a Success So down here is the error message So if it's if it's successful this is what we do if it's unsuccessful this is what we do [00:14:45](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h14m45s) And so that's just a quick look I mean obviously we can pause this video and Look at What I have done And then here is getUser when the page loads initially we get the id get the data packs it into some html And sends it off So This is the PHP side obviously We we do this create user and then there is a ajax [00:15:16](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h15rm16s) Site right in Javascript So In the JavaScript when the page loads Would you get user with the user ID We get the user ID from the field And then it loads that data that you saw If you if you looked no this is the wrong place Basically shows you that is the data it loads And that data can be edited Except over here [00:15:51](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h15m51s) You cannot add numbers in the username but you can do that and it will update it as you see and will say trhat is updated it Obviously if there is a Error with the email already used or something it will also give you an error And this is all part of how we basically have setup the JavaScript Ok we can scroll down to So I've just added to Javascript to the view file And here is get user the server side and there is the the function [00:16:24](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h16m24s) I have two functions usually one that sort of deals with Waiting and then eventually setting up the data into the The View And here's one making the call so this is how you call structure would usually look In index. php ?option = your components name And the task which Must go to the Ajax controller And then this is the task [00:16:54](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h16m54s) And you then here can stipulate what format you would like the result set returned in if you Want the result set returned the raw which in sometime sometimes is the case There is if you look here There is a raw Variable You can just set the raw variable to true Then it will give you the results set just Like that without Braces around them [00:17:23](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h17m23s) Or a call back or anything You see when you make calls from JavaScript It usually Wants this call back to be added If you make calls from other JavaScript libraries you might just want the braces around it But if you want it raw which also is often the case you just add the variable raw to the URL = true and then end up passing it back to you with this So that's a little heads up on Ajax function so [00:17:56](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h17m56s) the format must json that's Important Ok and always use a token Have the token on the page this token is necessary To prevent cross site scripting of course I usually add the token to the page Through the Java oh sorry php So there is a place down here if you scroll down where is it I just remembered remembered [00:18:28](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h18m28s) If you are working in a backend The backend the backend and the admin area you don't need to have the token its get added to the page dynamically Since we Basically parsed the JavaScript Areas And if we see token we actually know oh token is to be there and we add the token for you Where is the front end you must actually literally add the token There was just too many reasons for us Not to do the Automation in the front end so but you could literally change to the variable name as well [00:19:03](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h19m03s) To something of your choice Where is now it's just token it's backend it's more secure ok so You need to always add a token To the request and check whether the token is there if it's not that it wouldn't make a request at all Ok so I was looking at another function there Ok so that's simply how we create a user And integrate the user concept within this component I hope that was helpful I realize if you not that fluent in programming in JavaScript Or in jQuery [00:19:41](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h19m41s) As well as in PHP This might not fully resolved your questions Like I said where the beginning of these tutorials I wasn't planning on making a component that is Very easy to use but rather a Component that is very powerful to use And leaving it up to you to decide how to implement This user integration Is really the most powerful way of doing it At least we are adding the the class with which you can create the user And again even there you can go look at that class [00:20:20](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h20m20s) If you feel my implementation is a little you know needs some Inprovement You can either shoot me a mail or you could Take For this component Builder on GitHub Pull down to your own repository Make the changes push it back And The place inside Of component builder where this class is constructed [00:20:47](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h20m47s) is obviously Within The compilers files. Now the compiler Has Has quite a few files and you need to do do a search So you basically go to component Builder go to helper and then Use the compiler And here is all its files So do a search in here [00:21:09](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h21m09s) For the function Create user you could you know just search for that and I'm sure you're fine it and it is in that area where you need to make the adaptation and the changes because it's dynamically added It is not it can't be edited like right in the script it needs to be edit in the function where we add it Just go search those functions [00:21:38](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h21m38s) Thank you for watching That's how you create users and connect them to your components Obviously there is much more to say but I'm afraid this will get way too long like most of my tutorials get to long I want to keep it this this way and trying to make it shorter hope you enjoyed that +It uses the Joomla's default filtering method in the way that it gets values that are being posted. And then value dates that those values actually are there. Then sends the values over to the model. [00:11:05](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h11m05s) Now that's looking at it from a code side. If you look at the model it simply shows you various methods. Some of them are protected, and some of them are public. Those(getcreateuserfields) are usually the ones that gets called from the controller once the Ajax gets the verification level and it's verified. Here is the createuser class. [00:11:31](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h11m31s) We first check whether this user, (now this is custom script I mean this I wrote custom, component build does not write this) so basically when you go back to the user interface, you will see that if I scroll down here, create user fields, then there's create user. You see that I wrote this right here, and that basically gets placed into the model. [00:12:07](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h12m07s) So that's the Ajax method. In that Ajax input I annotate the specific tasks, the task name, and specific variables, and again the method name. You could have different method names from task names. What should be validated, whether it's integer, float or whatever. And if there's no value, what is the value by default. So this is where you set up there basically the controllers information. [00:12:39](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h12m39s) And then here your customisely write your script. So basically create user gets data and seems from what view is it, then validates with this current login user has the permission to the experts area. Then it converts the data because it's a json set of datasets that I'm sending across. Checks whether it's an array, does some other checks. + +### Global Helper Class + +So you can see I'm still using that [00:13:17](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h13m17s) global helper class and methods. Here are number of custom methods obviously that is available in that class. Go through it. We just add it in there as freebies and you don't want to always see the need of them but I'm using them often. Even if you have improvements on those methods, please make a pull request and we'll make those changes. Then over here(see video) once we've become very sure that we've got enough data on the right data. We basically get that same method and we need to create user. [00:13:55](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h13m55s) We get result. And if it's not the correct result we deal with it accordingly. And if it is the correct result we add the user to the user group. That's something that create user does not do. We have a user group setup [00:14:17](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h14m17s) basically in the component. We get that user group, set into the correct user group. This is a return message, if it's an error or it's a success. So down here(see video) is the error message. If it's successful this is what we do if it's unsuccessful this is what we do. [00:14:45](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h14m45s) That's just a quick look, I mean obviously we can pause this video, and look at what I have done. + +### Get User Field + +And then here is getUser when the page loads initially. We get the id, we get the data. Packs it into some html and sends it off. This is the php site obviously where we do this create user. Then there is a ajax [00:15:16](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h15rm16s) site in Javascript. In the JavaScript when the page loads, you would get user with the user id, we get the user id from the field. And then it loads that data that you saw. Basically shows you that is the data it loads. And that data can be edited except over here(username). [00:15:51](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h15m51s) You cannot add numbers in the username but you can do that(see video) and it will update it as you see and will say that it is updated. Obviously if there is a error with the email already used or something it will also give you an error. And this is all part of how we basically have setup the JavaScript. I've just added Javascript to the view file. Here is getuser on the server site, and there is the function. [00:16:24](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h16m24s) I have two functions usually one that deals with waiting and then eventually setting up the data into the view. Here is the one making the call. This is how the call structure would usually look in index.php?option=your components name, and the task which must go to the Ajax controller. And then this(getuser) is the task. [00:16:54](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h16m54s) And here you can stipulate what format you would like the result set returned in. If you want the result set returned raw, which in is sometimes the case, there is if you look here, there is a raw variable. You can just set the raw variable to true. Then it will give you the results set just like that without braces around them, [00:17:23](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h17m23s) or a call back or anything. You see when you make calls from JavaScript, it usually wants this call back to be added. If you make calls from other JavaScript libraries, you might just want the braces around it. But if you want it raw which also is often the case you just add the variable raw to the URL = true and then end up passing it back to you with this. So that's a little heads up on Ajax function. [00:17:56](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h17m56s) The format must stay json, that's important. + +### Use Tokens + +Always use a token. Have the token on the page. This token is necessary to prevent cross site scripting of course. I usually add the token to the page, through php. [00:18:28](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h18m28s) If you are working in a back end in the admin area, you don't need to have the token. It gets added to the page dynamically. Since we parsed the JavaScript areas. If we see token we actually know token is to be there and we add the token for you. Where as in the front end you must actually literally add the token. There was just too many reasons for us not to do the automation in the front end. You could literally change the variable name as well [00:19:03](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h19m03s) to something of your choice. Where as now it's just token, it's back end it's more secure. You need to always add a token to the request and check whether the token is there, if it's not that it wouldn't make a request at all. That's simply how we create a user and integrate the user concept within this component. + +I hope that was helpful. I realize if you not that fluent in programming in JavaScript or in jQuery [00:19:41](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h19m41s) as well as in php. This might not fully resolved your questions. Like I said where the beginning of these tutorials I wasn't planning on making a component that is very easy to use, but rather a component that is very powerful to use. And leaving it up to you to decide how to implement this user integration is really the most powerful way of doing it. At least we are adding the the class which you can create the user. Even there you can go look at that class. [00:20:20](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h20m20s) If you feel my implementation needs some improvement, you can either shoot me a mail or you could fork this component builder on GitHub. Pull it down to your own repository. Make the changes push it back. The place inside of component builder where this class is constructed, [00:20:47](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h20m47s) is within the compilers files. Now the compiler have quite a few files and you need to do a search. Go to component builder, go to helper, and then use the compiler. Here is all its files. Do a search in here [00:21:09](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h21m09s) for the function create user. Just search for that and I'm sure you're fine it. It is in that area where you need to make the adaptation and the changes because it's dynamically added. It can't be edited right in the script, it needs to be edit in the function where we add it. Just go search those functions. [00:21:38](https://www.youtube.com/watch?v=ckFakaQ90JY&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h21m38s) That's how you create users and connect them to your components. \ No newline at end of file