Skip to content

a code example for a simple workflow  #26

Open

Description

@dirkpetersen

I wonder if you could add an end to end code example that implements a simple workflow for temporary accounts

  • create user name x with random password y
  • create folder name x
  • give user x write access to folder x
  • create rule that files are deleted after successful download
  • create rule that user will be deactivated after x days

Activity

  1. dirkpetersen commented on Aug 18, 2020

    @dirkpetersen
    Author

    So it seems I can do all the api gets and I can also create a folder

    username = 'bobo'
    api = smartfile.BasicClient(apikey,password)
    ret=api.post('/path/oper/mkdir/', username)
    

    but when I want to create a user it does not work, these seem to be the required fields

    myuser={'username': username, 'name': username, 'email': 'test@test.org', 'password': 'badman123', 'role': 'User'}
    api.post('/user', myuser)
    
    
    ~/.local/lib/python3.8/site-packages/smartfile/__init__.py in _do_request(self, request, url, **kwargs)
         56         else:
         57             if response.status_code >= 400:
    ---> 58                 raise ResponseError(response)
         59         # Try to return the response in the most useful fashion given it's
         60         # type.
    
    ResponseError: Response 404: Error: b'{"detail":"Not found."}'
    
    

    when I try the post example at https://app.smartfile.com/api/2/user/ I get

    {
        "detail": "CSRF Failed: CSRF token missing or incorrect."
    }
    
  2. jalohse commented on Aug 18, 2020

    @jalohse

    You need to use the ** operator to unpack your myuser dictionary into kwargs. So:
    api.post('/user', **myuser)

  3. dirkpetersen commented on Aug 19, 2020

    @dirkpetersen
    Author

    Awesome, that worked. Thank you so much Jessica !!

  4. dirkpetersen commented on Aug 19, 2020

    @dirkpetersen
    Author

    Actually one more question about assigning a home directory to a user at creation time

    username='newuser23'
    myuser={'username': username, 'name': username, 'email': 'test@test.org', 'password': 'badman123', 'role': 'User', 'home': username}
    api.post('/user', **myuser)
    

    so the only thing I added is 'home', but I get this:

    ~/.local/lib/python3.8/site-packages/smartfile/__init__.py in _request(self, method, endpoint, id, **kwargs)
        108             trys += 1
        109             try:
    --> 110                 return self._do_request(request, url, **kwargs)
        111             except ResponseError as e:
        112                 if self.throttle_wait and e.status_code == 503:
    
    ~/.local/lib/python3.8/site-packages/smartfile/__init__.py in _do_request(self, *args, **kwargs)
        205         # Add the token authentication
        206         kwargs['auth'] = (self.key, self.password)
    --> 207         return super(BasicClient, self)._do_request(*args, **kwargs)
        208 
        209 
    
    ~/.local/lib/python3.8/site-packages/smartfile/__init__.py in _do_request(self, request, url, **kwargs)
         56         else:
         57             if response.status_code >= 400:
    ---> 58                 raise ResponseError(response)
         59         # Try to return the response in the most useful fashion given it's
         60         # type.
    
    ResponseError: Response 500: Error: b'{"detail":"An error has occurred and our team has been notified. If you need assistance, contact your administrator."}'
    
    
  5. dirkpetersen commented on Aug 19, 2020

    @dirkpetersen
    Author

    OK, when I set 'home': '/'+username instead of 'home': username the error went away. However, the home directory was not changed after that and was still set to '/' . Not sure what I am doing wrong

  6. dirkpetersen commented on Aug 19, 2020

    @dirkpetersen
    Author

    my last step was trying to give permissions to the new folder for the new user :

    I tried

    access = {'user': username, 'path': '/'+folder, 'read': True, 'list': True, 'remove': True, 'write': True}
    ret = api.post('/access/user', **access)
    

    or

    access = {'acl': {'read': True,'write': True, 'remove': True, 'list': True},
                      'recursive': True,
                      'path': '/'+folder,
                       'user': username}
    ret = api.post('/access/user', **access)
    

    but in both cases I got this error:

      File "app/bin/share-sftp", line 45, in <module>
        ret = api.post('/access/user', **access)
      File "/app/lib/python3.6.9/lib/python3.6/site-packages/smartfile/__init__.py", line 131, in post
        return self._request('post', endpoint, id=id, data=kwargs)
      File "/app/lib/python3.6.9/lib/python3.6/site-packages/smartfile/__init__.py", line 110, in _request
        return self._do_request(request, url, **kwargs)
      File "/app/lib/python3.6.9/lib/python3.6/site-packages/smartfile/__init__.py", line 207, in _do_request
        return super(BasicClient, self)._do_request(*args, **kwargs)
      File "/app/lib/python3.6.9/lib/python3.6/site-packages/smartfile/__init__.py", line 58, in _do_request
        raise ResponseError(response)
       smartfile.errors.ResponseError: Response 500: Error: b'{"detail":"An error has occurred and our team has been notified. If you 
       need assistance, contact your administrator."}'
    

    then finally I tried :

    access = 'user=dirk11&path=%2Fdirk11&read=true&list=true&remove=true&write=true'
    ret = api.post('/access/user?'+access)
    

    and I did not get any error message but the permission change was not saved / applied

    part of my challenge is perhaps that when try to use the api.post option here https://app.smartfile.com/api/2/access/user/ I get

    image

    and if I try to access https://app.smartfile.com/api/2/access/path/

    I am getting

    image

    I tried googling for "api.post(" examples and found 173 hits but I was not able to find one example that does a permission change

  7. jalohse commented on Aug 19, 2020

    @jalohse

    For the home folder itself, it cannot be set when first creating a user. The user's filesystem has not been created yet, so we cannot set a home folder. That call will need to be made after the user has been created to /user/username after the folder is created in their account. If your account has admin view, you can set the home folder through the start_folder_path parameter.

    I am assuming your account is what we call a NextGen account. In NextGen, /access isn't used, as each user has their own filesystem, and thus would have all permissions. NextGen utilizes shares, where one user can share one of their folders with another user and set permissions there.

  8. jalohse commented on Aug 19, 2020

    @jalohse

    An easier way to view how these actions should be made is to inspect the network calls the SmartFile UI uses and model your calls after that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions