List | Create | Modify and Delete Users directly from Python controller without using HMI

91 views
Skip to first unread message

Nicolas Hiblot

unread,
Jun 2, 2020, 5:57:31 PM6/2/20
to web2py-users
Hello All,

I have read a lot of cases and uses cases about web2py users.
But my case is a little différent ;
I would like to

List | Create  | Modify and Delete Users directly from Python controller without using HMI

My user db is always the same
login 1 is user0
login 2 is user1
and I can have a lot of users but user0 and user1 are always here

1) List all users by id and username without user0 and user1

    try :
        listusers = db(db.auth.............
        if len(listusers) == 0:
    except Exception as e:
        listusers = []


2) Create a user with a mail / login / and Password directly using python method in the controller with the db (without the GUI)


3) Modify a password of an already created user directly using python method in the controller with the db (without the gui)
Note : I have already the id and/or the username of the user

4) Delete an already created user directly using python method  in the controller with the db (without the gui)
Note : I have already the id and/or the username of the user


Thanks in advance
Regards



Nicolas Hiblot

unread,
Jun 3, 2020, 5:25:39 PM6/3/20
to web2py-users
Hello All,

I really don't know if it's possible !
But if it is, could someone please help me to do that ??

Dave S

unread,
Jun 3, 2020, 11:44:34 PM6/3/20
to web2py-users


On Wednesday, June 3, 2020 at 2:25:39 PM UTC-7, Nicolas Hiblot wrote:
Hello All,

I really don't know if it's possible !
But if it is, could someone please help me to do that ??


You want to become familiar with the Auth() tools;

There's a simple adduser.py floating around the group archives , it uses auth.register_bare(). There's a brief discussion on this topic at 

Also, depending on your front end (that is, if you're using nginx or such instead of Rocket), you may want to have some of the authentication performed there.  I'm still studying how to do this for SSL, so I'm not sure I can provide any details, but your front end documentation would be a starting place.

/dps

Val K

unread,
Jun 4, 2020, 4:46:14 PM6/4/20
to web2py-users
db.auth_user is just a table like any other
1) 
db(~db.auth_user.id.belongs([1,2])).select(...)
or
 db
(~db.auth_user.username.belongs(['user0', 'user1'])).select(...)

note that `~`  inverts `belongs` i.e. it is equal sql  -`NOT IN` 

2)
 db.auth_user.insert(username = 'John', ...)
- nothing new except  only password-value  - you should use this magic to convert password to hash: 
 str(CRYPT()('some_password')[0])
also you can pass id-value if you need:
   db.auth_user.insert(id = 123, username = 'John', ..., password = str(CRYPT()('11&888')[0]) )

3) db(db.auth_user.id == 1).update(password = str(CRYPT()('newpassword')[0]))

4) db(db.auth_user.username == 'John').delete()
but be careful - web2py uses ondelete='CASCADE' by default, so deleting user-John also deletes all records from all tables (having ondelete='CASCADE') that reference to John-id
you can use this
db(db.auth_user.username == 'John').update(registration_key='blocked')
to ban user (registration_key='' to unban

Nicolas Hiblot

unread,
Jun 4, 2020, 4:50:15 PM6/4/20
to web...@googlegroups.com
Thanks Dave and Val
It's perfect !!
Web2py forever

AGRogers

unread,
Jun 7, 2020, 9:36:35 PM6/7/20
to web...@googlegroups.com
Yes, thank you too. I will need that.

Le mardi 2 juin 2020 23:57:31 UTC+2, Nicolas Hiblot a écrit :

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/23b5c0d8-6366-41c8-ad58-85504a4efe8do%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages