def apply_actions(self, user, options): if options.get('admin'): user.is_superuser = True elif options.get('noadmin'): user.is_superuser = False if options.get('reject'): reject_reason = options.get('reject_reason', None) res = user_action.reject(user, reject_reason) if res.is_error(): self.stderr.write("Failed to reject: %s\n" % res.message) else: self.stderr.write("Account rejected\n") if options.get('verify'): res = user_action.verify(user, user.verification_code) if res.is_error(): self.stderr.write("Failed to verify: %s\n" % res.message) else: self.stderr.write("Account verified (%s)\n" % res.status_display()) if options.get('accept'): res = user_action.accept(user) if res.is_error(): self.stderr.write("Failed to accept: %s\n" % res.message) else: self.stderr.write("Account accepted and activated\n") if options.get('active'): res = user_action.activate(user) if res.is_error(): self.stderr.write("Failed to activate: %s\n" % res.message) else: self.stderr.write("Account %s activated\n" % user.username) elif options.get('inactive'): inactive_reason = options.get('inactive_reason', None) res = user_action.deactivate(user, inactive_reason) if res.is_error(): self.stderr.write("Failed to deactivate: %s\n" % res.message) else: self.stderr.write("Account %s deactivated\n" % user.username) invitations = options.get('invitations') if invitations is not None: user.invitations = int(invitations) groupname = options.get('add-group') if groupname is not None: try: group = Group.objects.get(name=groupname) user.groups.add(group) except Group.DoesNotExist, e: self.stderr.write( "Group named %s does not exist\n" % groupname)
def apply_actions(self, user, options): if options.get('admin'): user.is_superuser = True elif options.get('noadmin'): user.is_superuser = False if options.get('reject'): reject_reason = options.get('reject_reason', None) res = user_action.reject(user, reject_reason) if res.is_error(): self.stderr.write("Failed to reject: %s\n" % res.message) else: self.stderr.write("Account rejected\n") if options.get('verify'): res = user_action.verify(user, user.verification_code) if res.is_error(): self.stderr.write("Failed to verify: %s\n" % res.message) else: self.stderr.write("Account verified (%s)\n" % res.status_display()) if options.get('accept'): res = user_action.accept(user) if res.is_error(): self.stderr.write("Failed to accept: %s\n" % res.message) else: self.stderr.write("Account accepted and activated\n") if options.get('active'): res = user_action.activate(user) if res.is_error(): self.stderr.write("Failed to activate: %s\n" % res.message) else: self.stderr.write("Account %s activated\n" % user.username) elif options.get('inactive'): inactive_reason = options.get('inactive_reason', None) res = user_action.deactivate(user, inactive_reason) if res.is_error(): self.stderr.write("Failed to deactivate: %s\n" % res.message) else: self.stderr.write("Account %s deactivated\n" % user.username) invitations = options.get('invitations') if invitations is not None: user.invitations = int(invitations) groupname = options.get('add-group') if groupname is not None: try: group = Group.objects.get(name=groupname) user.groups.add(group) except Group.DoesNotExist, e: self.stderr.write("Group named %s does not exist\n" % groupname)
def test_reactivation(self): """Test activation/deactivation logic.""" # Verify the user. res = verify(self.user1, self.user1.verification_code) self.assertFalse(res.is_error()) # We cannot deactivate an unmoderated user. ok, _ = validate_user_action(self.user1, "DEACTIVATE") self.assertFalse(ok) res = deactivate(self.user1) self.assertTrue(res.is_error()) # Accept the user. res = accept(self.user1) self.assertFalse(res.is_error()) # Check if we can deactivate properly an active user. ok, _ = validate_user_action(self.user1, "DEACTIVATE") self.assertTrue(ok) res = deactivate(self.user1) self.assertFalse(res.is_error()) # This should be able to happen many times. ok, _ = validate_user_action(self.user1, "DEACTIVATE") self.assertTrue(ok) res = deactivate(self.user1) self.assertFalse(res.is_error()) # Check if we can activate properly an inactive user ok, _ = validate_user_action(self.user1, "ACTIVATE") self.assertTrue(ok) res = activate(self.user1) self.assertFalse(res.is_error()) # This should be able to happen only once. ok, _ = validate_user_action(self.user1, "ACTIVATE") self.assertFalse(ok) res = activate(self.user1) self.assertTrue(res.is_error())
def test_accept(self): """Test acceptance logic.""" # Verify the user first. res = verify(self.user1, self.user1.verification_code) self.assertFalse(res.is_error()) # Test if check function works properly for unmoderated user. ok, _ = validate_user_action(self.user1, "ACCEPT") self.assertTrue(ok) # Test if accept action works properly for unmoderated user. res = accept(self.user1) self.assertFalse(res.is_error()) self.assertEqual(len(mail.outbox), 1) # Test if check function fails properly for moderated user. ok, _ = validate_user_action(self.user1, "ACCEPT") self.assertFalse(ok) # Test if accept action fails properly for moderated user. res = accept(self.user1) self.assertTrue(res.is_error()) self.assertEqual(len(mail.outbox), 1) # Test if the rest of the actions can apply on a moderated user. # User cannot be rejected. ok, _ = validate_user_action(self.user1, "REJECT") self.assertFalse(ok) res = reject(self.user1, 'Too late') self.assertTrue(res.is_error()) # User cannot be reactivated. ok, _ = validate_user_action(self.user1, "ACTIVATE") self.assertFalse(ok) res = activate(self.user1) self.assertTrue(res.is_error())
def user_action(request, user_id): admin_id = request.user_uniq req = api.utils.get_json_body(request) logger.info('user_action: %s user: %s request: %s', admin_id, user_id, req) if 'activate' in req: try: user = AstakosUser.objects.select_for_update().get(uuid=user_id) except AstakosUser.DoesNotExist: raise faults.ItemNotFound("User not found") user_logic.activate(user) user = AstakosUser.objects.get(uuid=user_id) user_data = {'id': user.uuid, 'is_active': user.is_active} data = json.dumps({'user': user_data}) return http.HttpResponse(data, status=200, content_type='application/json') if 'deactivate' in req: try: user = AstakosUser.objects.select_for_update().get(uuid=user_id) except AstakosUser.DoesNotExist: raise faults.ItemNotFound("User not found") user_logic.deactivate(user, reason=req['deactivate'].get('reason', None)) user = AstakosUser.objects.get(uuid=user_id) user_data = {'id': user.uuid, 'is_active': user.is_active} data = json.dumps({'user': user_data}) return http.HttpResponse(data, status=200, content_type='application/json') if 'renewToken' in req: try: user = AstakosUser.objects.select_for_update().get(uuid=user_id) except AstakosUser.DoesNotExist: raise faults.ItemNotFound("User not found") user.renew_token() user.save() user_data = { 'id': user.uuid, 'auth_token': user.auth_token, } data = json.dumps({'user': user_data}) return http.HttpResponse(data, status=200, content_type='application/json') if 'signTerms' in req: try: user = AstakosUser.objects.select_for_update().get(uuid=user_id) except AstakosUser.DoesNotExist: raise faults.ItemNotFound("User not found") user.has_signed_terms = True user.date_signed_terms = datetime.now() user.save() user_data = { 'id': user.uuid, 'has_signed_terms': user.has_signed_terms } data = json.dumps({'user': user_data}) return http.HttpResponse(data, status=200, content_type='application/json') raise faults.BadRequest("Invalid action")
def user_action(request, user_id): admin_id = request.user_uniq req = api.utils.get_json_body(request) logger.info('user_action: %s user: %s request: %s', admin_id, user_id, req) if 'activate' in req: try: user = AstakosUser.objects.select_for_update().get(uuid=user_id) except AstakosUser.DoesNotExist: raise faults.ItemNotFound("User not found") user_logic.activate(user) user = AstakosUser.objects.get(uuid=user_id) user_data = { 'id': user.uuid, 'is_active': user.is_active } data = json.dumps({'user': user_data}) return http.HttpResponse(data, status=200, content_type='application/json') if 'deactivate' in req: try: user = AstakosUser.objects.select_for_update().get(uuid=user_id) except AstakosUser.DoesNotExist: raise faults.ItemNotFound("User not found") user_logic.deactivate( user, reason=req['deactivate'].get('reason', None)) user = AstakosUser.objects.get(uuid=user_id) user_data = { 'id': user.uuid, 'is_active': user.is_active } data = json.dumps({'user': user_data}) return http.HttpResponse(data, status=200, content_type='application/json') if 'renewToken' in req: try: user = AstakosUser.objects.select_for_update().get(uuid=user_id) except AstakosUser.DoesNotExist: raise faults.ItemNotFound("User not found") user.renew_token() user.save() user_data = { 'id': user.uuid, 'auth_token': user.auth_token, } data = json.dumps({'user': user_data}) return http.HttpResponse(data, status=200, content_type='application/json') if 'signTerms' in req: try: user = AstakosUser.objects.select_for_update().get(uuid=user_id) except AstakosUser.DoesNotExist: raise faults.ItemNotFound("User not found") user.has_signed_terms = True user.date_signed_terms = datetime.now() user.save() user_data = { 'id': user.uuid, 'has_signed_terms': user.has_signed_terms } data = json.dumps({'user': user_data}) return http.HttpResponse(data, status=200, content_type='application/json') raise faults.BadRequest("Invalid action")