aleph
aleph copied to clipboard
FEATURE: Group creation and management - quick workaround
I'm aware this is being discussed and that many great individuals are putting in the time to do this: #1852 #1522 More than a suggestion of a feature, which is, of course, something many are working on, I wanted to offer admins a quick workaround to create groups without implementing the code included in aleph/tests/util.py into the main production framework.
After reviewing the documentation I wasn't able to figure out how to create groups using the command line or any other method (which is a feature not yet implemented, and understand it's hard work - no reproach intended). However, after reviewing the code in aleph/tests and working around the console in my production docker I was able to implement code which was already contained in your excellent work.
More than requesting a feature I wanted to offer other perplexed admins a quick workaround which helped us enormously. I'm sure there might be a better and more developed versions of this and would love to hear from others and more experienced users/devs. Happy to accept my workaround is not the best approach. Here's what I did:
- I first cd'd to my aleph docker
cd /path/aleph - I then ran a python shell inside docker:
docker-compose run --rm shell aleph shella terminal prompt should appear>>> - inside this console run the following set of commands (all are taken from existing code in the project, nothing is my creation here)
from aleph.model import Rolefrom aleph.model import db
group = Role.load_or_create("yourGroup", Role.GROUP, "yourGroup") #this creates an abstract group that is not yet saved in the db
USR = Role.by_email("[email protected]") #we invoke a user by their email
USR.add_role(group) #we add them to the group abstractly
db.session.commit() #and we add it to the database, the group should now show in the api and in the user's main UI.
use quit() to exit the python console
I'm aware there might be much better ways to do this, but it may help others find a quick fix to create groups, and hope this is helpful to all as we wait (or help) for a feature in the command line. :) I did not know where else to post this, and decided -as it is not a bug- to leave it here.
Update: Managing groups In the same fashion as before, if sys-admins ever need to remove someone from a group, or remove admin capabilities from a user you may do the following:
- I first cd'd to my aleph docker
cd /path/aleph - I then ran a python shell inside docker:
docker-compose run --rm shell aleph shella terminal prompt should appear>>> - inside this console run the following set of commands (all are taken from existing code in the project, nothing is my creation here)
from aleph.model import Role
from aleph.model import db
USR = Role.by_email("[email protected]") # Find a user by email. There are other methods, but this may be the simplest for most.
USR.roles # this will list the groups and roles a user belongs to... for instance:
[<Role(6, "group1")>, <Role(7, "group2")>] # this is a regular python list containing the Role class for groups.
If you are unfamiliar with Python, number counts begin at 0; so, group1 would be position 0 and group2 position 1. Keep this in mind before moving anything, this may pose issues for your user-group database. Let us suppose you would like to remove USR from group2:
USR.roles.pop(1) # As with any Python list this will "pop" (remove) the list element in position 1 (group2)
db.session.commit() # Remember to save these changes into the database. Everything else exists only abstractly, and needs to be saved.
Removing admin capabilities from an admin user:
from aleph.model import Role
from aleph.model import db
USR = Role.by_email("[email protected]") # Find a user by email. There are other methods, but this may be the simplest for most.
USR.is_admin # if the user is currently an admin, this should show True
USR.is_admin = False # this changes the value of the is_admin to False (to not an admin). You could do the opposite to assign admin capabilities to a user by setting it to True instead of False
db.session.commit() # always remember to save in the user-group database.
I hope these steps help, and are not too overly complicated.
Update: Changing a user's password
from aleph.model import Role
from aleph.model import db
USR = Role.by_email("[email protected]") # Find a user by email. There are other methods, but this may be the simplest for most.
USR.set_password("new_password") #Set the new password
db.session.commit() # always remember to save in the user-group database.
@sunu @pudo How do we feel about adding this to our documentation?
@samsamros Thanks for putting this together. Really appreciate it!
@Rosencrantz you are very welcome! I'm glad (and relieved) this was useful! Would it also be useful to post how we managed to remove users from groups, and add and remove admin capabilities using this very method (with slight modifications)? If so, I would be happy to help.
@samsamros Absolutely! If you have the time to put something together, we'd really appreciate it
@Rosencrantz I'd be glad to contribute! would an edit to the original post be better? or 1) a new post, or 2) a comment in this thread?
@Rosencrantz Added a few additional steps. I hope these are helpful.