Skip to content
Snippets Groups Projects
Commit fa0c42ef authored by Jean-Baptiste Pasquier's avatar Jean-Baptiste Pasquier
Browse files

feature: create_distant_admin command

parent 5ee11b1d
No related branches found
No related tags found
No related merge requests found
Pipeline #10088 passed
......@@ -167,6 +167,14 @@ AUTH_USER_MODEL = 'djangoldp_account.LDPUser'
DISTANT_LOGIN = True
```
### Create an administrator account for a distant user
You can create an administrator account for a distant user with the `create_distant_admin` command:
```bash
./manage.py create_distant_admin --urlid="http://server/users/xyz/"
```
## Import users in batch mode
To create a huge number of accounts from an existing database, you can use the `import_csv` command. The imported CSV file must contain user first name in the first column, user last name in the second column and user email in the third column. For example ;
......
import re
import requests
import uuid
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth import get_user_model
from urllib.parse import urlparse
class Command(BaseCommand):
help = 'Create a distant user backlink and make it administrator'
def add_arguments(self, parser):
parser.add_argument('--urlid', type=str, default="null", help='Urlid of the distant user')
def handle(self, *args, **options):
if(options['urlid']):
try:
distant_request = requests.get(options['urlid'])
except:
self.stdout.write(self.style.ERROR('Error. '+options['urlid']+" is unreachable"))
exit(1)
if(distant_request.status_code == 404):
self.stdout.write(self.style.ERROR('Error. '+options['urlid']+" is not found"))
exit(1)
else:
try:
distant_user = distant_request.json()
urlid = distant_user['@id']
email = distant_user['email']
username = distant_user['username']
except:
self.stdout.write(self.style.WARNING('Error while reading '+options['urlid']))
exit(1)
try:
netloc = re.sub(r'[\W_]+', '', urlparse(urlid).netloc)
user = get_user_model().objects.update_or_create(
urlid=urlid,
defaults={
'is_staff': True,
'is_superuser': True,
'email': email + "-" + netloc,
'username': username + "-" + netloc
}
)
self.stdout.write(self.style.SUCCESS('Successful created distant administrator: '+urlid))
except:
e
self.stdout.write(self.style.WARNING('Unable to save '+urlid+" as administrator"))
exit(0)
else:
self.stdout.write(self.style.WARNING('No urlid provided'))
exit(1)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment