benbyf Posted July 11, 2016 Posted July 11, 2016 Hi! I've been making my first modules and I've created three so far to help me learn. I would love so feedback or pull requests for improvements as I hope to write a tutorial about my work soon. In particular the third modules isn't very finished. git: https://github.com/benbyford/PW-starter-modules/ HelloUserYouSaved - adds message {your user name, page saved} in admin when a page is saved. This module shows how to implement a basic module, get and use variables, create a message in the admin RedirectAdminPages - redirect specific user role to a custom page set in the module config. This module shows how to implement module configuration, using variables saved in the admin, redirecting a user using session->redirect() HotSwapUser - Swap user on the fly in the admin or frontend of your site This module shows how users can be used in a module how to set a user permission how to install / uninstall something within your module how to create a function that can be output in the frontend of your site. 10
horst Posted July 11, 2016 Posted July 11, 2016 Hi! Very good initiative! I have skimread through the code and my opinion is: The first and second ones looks good. But to be honest, this hotswap user thing is a big security issue. Sorry! Also only for demonstration / tutorial purposes, it is better not to use hot user swapping as a thema. The audience for beginner tutorials often will be, yes, the name says it: beginners. And assumed they simply do not know about such security issues and best practices in regard of how to validate user-inputs, you should not show code like this to them. At least you would need to embedd all security relevant stuff too. But then it fastly will become to complex for a beginner tutorial. A good one would be to only pick up how to validate user inputs and how PW provides good tools for that. (type and format validation, whitelist matching, ...) With your code, everyone can login with each existing account (also Superuser) just by knowing / trying (multiple til endless!) user name(s). In regard to this, you may have a look into ALIF or TracyDebugger, how the security for this is tackled there. I hope you do not mind me my frankness. ------------- Looking to the code of the users redirect module, you can shorten it a bit. When found a matching user / role, you don't need to store true in a temporary var, break the loop and then check the temporary vars value / state, if you should redirect the current user. You simply can redirect the current user if a role matches: /* * ___renderPageRedirect * * redirect user to page if current user role found in config */ public function ___renderPageRedirect($event) { // check to see if current page = admin template page // otherwise: early return! if($this->page->template != "admin") { return; } // find roles set in module configuration and create array $roles = explode(',', $this->userRoles); // for each user in module config check to see if current user foreach ($roles as $key => $val) { $roles[$key] = trim($roles[$key]); // if current user found then redirect = true if($this->user->hasRole($roles[$key])) { // this will leave the page and code immediately! $this->session->redirect($this->redirectPage); // stop user search break; // this can stay in here, just as fallback, // if something went wrong with calling the redirect, // but normally this line will not get executed. } } } 4
benbyf Posted July 11, 2016 Author Posted July 11, 2016 Hi Horst! Thanks for you code amends, will alter now. I totally agree with you, the third module not really beginner level and needs a heck load more work on it before anyone was to actually use it. The security aspect I'm looking at next and thanks for those links. Bar the first module, I was trying to make modules that didn't already exist that would be useful and interesting for me to make and had aspects I could teach. If you have any more suggestions for a third module whcih doesn't already exist then I'm all ears. Thanks, 2
flydev Posted July 11, 2016 Posted July 11, 2016 8 minutes ago, benbyf said: If you have any more suggestions for a third module whcih doesn't already exist then I'm all ears. You could write a beginner module about Fieldtype/Inputfield which accept a simple text, save it and render its markup . As Horst said, nice initiative ! 5
benbyf Posted July 11, 2016 Author Posted July 11, 2016 anyone happen to have any ideas for inputfields that we havent already got?? 1
LostKobrakai Posted July 11, 2016 Posted July 11, 2016 A simple fieldtype might be "Name", where you enter a firstname and a lastname, which is stored separately and maybe a computed property for the fullname. I think it shouldn't hold more values, as it'll probably become complex just for the amount of fields. Also interesting might be other often extended core classes/modules like Process or Textformatter, with the latter actually being quite simple with often just a single method. 4
benbyf Posted July 12, 2016 Author Posted July 12, 2016 I've updated the my modules replacing HotUserSwap with simple TextformatterFindReplace instead. I'm going to work on some field types next 3
kongondo Posted July 12, 2016 Posted July 12, 2016 It would be good to also work on some Modules that do not 'autoload' Thanks for your efforts.... 1
benbyf Posted July 13, 2016 Author Posted July 13, 2016 Ok been trying to take on everyones comments. In doing so I've split repos into beginner and intermediate. https://github.com/benbyford/PW-starter-modules/https://github.com/benbyford/PW-intermediate-modules In doing so I can add to them with simple and more complex modules as and when I create them (and learn a new skill). The basic ones are currently super basic, so could probably do a couple more and then stop of crucial things to learn in modeul making. Any more suggestions welcome. 5
LostKobrakai Posted July 14, 2016 Posted July 14, 2016 This might be a nice quick to grasp intro to using composer dependencies for a fieldtype (and creating a fieldtype): https://github.com/LostKobrakai/FieldtypeCountrySelect 2
benbyf Posted July 14, 2016 Author Posted July 14, 2016 I think composer and dependency mamgement in general is a little out of scope for this series of module creation tutorials, looks interesting though, I've never felt the need for this myself mostly working on small projects and happy using profile exporting and wireshell for different installing / exporting tasks.
fuzendesign Posted July 25, 2016 Posted July 25, 2016 Would love to see a simple tut on using hooks from a front-end form. Like if a form creates or saves a PW page, can I take the value of a user ID in that saved form/page, and then use that ID to update an altogether different page.
fuzendesign Posted July 25, 2016 Posted July 25, 2016 Forgot to say thanks, though. I'm going to dive into the first 2 you created sometime this week. Thanks kindly.
benbyf Posted August 12, 2016 Author Posted August 12, 2016 Added new module PageDeferredPublish to Intermediate modules - Module allows you to publish a page at a time interval in the future e.g. 24hrs. adds Publish Later buttons to edit page and page tree uses LazyCron to count down pages that are both unpublished and have been checked to publish later when page count down reaches 0, page publishes 7
benbyf Posted August 12, 2016 Author Posted August 12, 2016 Also, added super simple ProcessSimpleAdminPage module to Intermediate modules – to show adding admin pages within a module. 5
benbyf Posted August 23, 2016 Author Posted August 23, 2016 Newest tutorial about modules now available on tuts+ - Extending the ProcessWire Admin Using Custom Modules http://webdesign.tutsplus.com/tutorials/extending-the-processwire-admin-using-custom-modules--cms-26863 Thanks for everyones help. 8
szabesz Posted August 24, 2016 Posted August 24, 2016 @benbyf Thanks a lot! I really appreciate the work you put into your tutorials.
arjen Posted August 24, 2016 Posted August 24, 2016 (edited) Great work @benbyf! I understand you wrote the tutorial to show that the module can do stuff on installing and uninstalling. There is another example you could use to handle creating pages and permissions (more info): public static function getModuleInfo() { return array( // Some default stuff 'title' => 'Title of the module', 'version' => 001, 'summary' => 'Summary of the module', 'autoload' => false, 'singular' => true, 'icon' => 'bookmark', 'permission' => 'name-of-the-permission', // Creates the page (and removes after uninstalling) 'page' => array( 'name' => 'name-of-the-page', 'parent' => 'admin', // Or whatever 'title' => 'Title of the Page', ), // Creates the permission (and removes after uninstalling) 'permissions' => array( 'name-of-the-permission' => 'Title of the permission' ), ); } Written in browser, so not tested. I guess you'll get the idea Edited August 24, 2016 by arjen link added 5
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now