def user(self, *parameters): """ shows or change your current biicode user """ parser = argparse.ArgumentParser(description=BiiCommand.user.__doc__, prog="bii user") parser.add_argument("name", nargs='?', default=None, help='Username you want to use. ' 'It should be already registered in http://www.biicode.com. ' 'If no name is provided it will show the current user.') parser.add_argument("-p", "--password", help='User password') args = parser.parse_args(*parameters) # To enable -h user, _ = self.bii.user_cache.localdb.get_login() if not args.name: anon = '(anonymous)' if not user else '' self.bii.user_io.out.info('Current user: %s %s' % (user, anon)) else: new_user = BRLUser(args.name) new_user = None if new_user == 'none' else new_user anon = '(anonymous)' if not new_user else '' if args.password is not None: token = self.bii.biiapi.authenticate(new_user, args.password) else: token = None if new_user == user: self.bii.user_io.out.info('Current user already: %s %s' % (user, anon)) else: self.bii.user_io.out.info('Change user from %s to %s %s' % (user, new_user, anon)) # TOOD: This temporary implementation, just cleans the database. A more efficient # implementation might just swap self.bii.user_cache.localdb.clean() self.bii.user_cache.localdb.set_login((new_user, token))
def deserialize(doc): brl = BRLUser(doc[User.SERIAL_ID_KEY]) user = User(brl) user._encrypted_password = doc[User.SERIAL_ENCRYPTED_PASSWORD] user.password_timestamp = doc.get(User.SERIAL_PASSWORD_TIMESTAMP, None) if User.SERIAL_NUMERIC_ID in doc: user.numeric_id = ID.deserialize(doc[User.SERIAL_NUMERIC_ID]) # Profile fields user.firstname = doc.get(User.SERIAL_FIRSTNAME, None) user.lastname = doc.get(User.SERIAL_LASTNAME, None) user.country = doc.get(User.SERIAL_COUNTRY, None) user.description = doc.get(User.SERIAL_DESCRIPTION, None) user.email = doc.get(User.SERIAL_EMAIL, None) user.visible_email = doc.get(User.SERIAL_VISIBLE_EMAIL, 0) == 1 user.allow_mailing = doc.get(User.SERIAL_ALLOW_MAILING, 0) == 1 user.active = doc.get(User.SERIAL_ACTIVE, 0) == 1 user.staff = doc.get(User.SERIAL_STAFF, 0) == 1 user.joined_date = UtcDatetime.deserialize( doc.get(User.SERIAL_JOINED_DATE, None)) user.confirmation_date = UtcDatetime.deserialize( doc.get(User.SERIAL_CONFIRMATION_DATE, None)) user.confirmation_token = doc.get(User.SERIAL_CONFIRMATION_TOKEN, None) # Old workspace methods user.block_counter = doc.get(User.SERIAL_MOD_COUNTER, 0) blocks_data = doc.get(User.SERIAL_BLOCKS, {}) user.blocks = DictDeserializer( BRLBlock, BlockMetaInfoDeserializer).deserialize(blocks_data) user.administrators = Permissions.deserialize( doc.get(User.SERIAL_ADMINISTRATORS, {})) social_accounts_doc = doc.get(User.SERIAL_SOCIAL_ACCOUNTS) user.social_accounts = DictDeserializer( str, SocialAccount).deserialize(social_accounts_doc) # Achievements user.read_api_counter = doc.get(User.SERIAL_READ_API_COUNTER, 0) user.publish_counter = doc.get(User.SERIAL_PUBLISH_COUNTER, 0) user.reuse_counter = doc.get(User.SERIAL_REUSE_COUNTER, 0) # Additional profile fields user.street_1 = doc.get(User.SERIAL_STREET1, "") user.street_2 = doc.get(User.SERIAL_STREET2, "") user.city = doc.get(User.SERIAL_CITY, "") user.postal_code = doc.get(User.SERIAL_POSTAL_CODE, "") user.region = doc.get(User.SERIAL_REGION, "") user.tax_id = doc.get(User.SERIAL_TAX_ID, "") user.vat = doc.get(User.SERIAL_VAT, "") # OAuth user.oauth_github_token = doc.get(User.SERIAL_OAUTH_GITHUB_TOKEN, None) user.oauth_google_token = doc.get(User.SERIAL_OAUTH_GOOGLE_TOKEN, None) # Max workspace size, default BII_MAX_USER_WORKSPACE_SIZE user.max_workspace_size = doc.get(User.SERIAL_MAX_WORKSPACE_SIZE, BII_MAX_USER_WORKSPACE_SIZE) # Invited by user.invited_by = doc.get(User.SERIAL_INVITED_BY, None) return user
def __new__(cls, name, validate=True): if not validate: obj = str.__new__(cls, name) else: original_name = name try: name = name.strip().replace('\\', '/') tokens = name.split('/') if '' in tokens: raise InvalidNameException( '%s is not a valid %s. It has trailing slashes' % (name, cls.__name__)) if len(tokens) > 2: raise InvalidNameException( '%s is not a valid %s. ' 'It should be in the form user/name' % (name, cls.__name__)) user = tokens[0] obj = str.__new__(cls, user + '/' + tokens[1]) obj._name = ComplexName(tokens[1]) obj._user = BRLUser(user) except IndexError: raise InvalidNameException('%s is not a valid %s' % (original_name, cls.__name__)) return obj
def deserialize(data): """deserialize model""" theid = BRLUser(data[UserSubscription.SERIAL_ID_KEY]) ret = UserSubscription(theid) ret.customer_id = data.get(UserSubscription.SERIAL_CUSTOMER_ID_KEY, None) ret.plan_id = data.get(UserSubscription.SERIAL_PLAN_ID, FREE_PLAN_ID) return ret
def make_test_user(self): uid = self._db.generate_user_id() name = BRLUser('TestUser%d' % uid) user = User(name) user.password = '******' user.active = True self._db.create_user(user, uid) return user
def testLowerCaseBackslashConversion(self): brl = BRLBlock("Drodri\Pedro/geOm\Mybranch") self.assertEqual(BRLUser('Drodri'), brl.owner) self.assertEqual('Drodri', brl.owner) self.assertEqual('Pedro/geOm', brl.block_name) self.assertEqual(BlockName('Pedro/geOm'), brl.block_name) self.assertEqual(ComplexName('mybranch'), brl.branch) self.assertEqual('mybranch', brl.branch)
def setUp(self): """ all find tests have a user, store, and BiiService """ self.user = BRLUser('find_user') self.store = TestingMemServerStore() user = User(self.user) user.password = '******' self.store.create_user(user) self.service = BiiService(self.store, self.user)
def testParsing(self): brl = BRLBlock("drodri/pedro/geom/mybranch") self.assertEqual(BRLUser('drodri'), brl.owner) self.assertEqual('drodri', brl.owner) self.assertEqual('pedro/geom', brl.block_name) self.assertEqual(BlockName('pedro/geom'), brl.block_name) self.assertEqual(ComplexName('mybranch'), brl.branch) self.assertEqual('mybranch', brl.branch)
def test_change_subscription(self): # Create users for test self.store.create_user(User("cooper")) ensure = Security("cooper", self.store) self._subscribe("cooper", "startup_35_5_x") # Coop has a private block private_brl = BRLBlock("cooper/cooper/private_block/master") self._add_block_to_user("cooper", private_brl, True) for tmp in xrange(4): new_user = BRLUser("user%s" % tmp) ensure.check_grant_read_or_write_permissions_to(new_user, private_brl) bper = self.store.read_block_permissions(private_brl) bper.write.grant(new_user) self.store.upsert_block_permissions(bper) # Try to downgrade to personal plan with self.assertRaisesRegexp(ForbiddenException, "You are currently using 4 users, " "reduce to 1 before plan downgrade"): ensure.check_can_change_current_subscription("cooper", "personal_7_1_x") # Remove collaborators to 1 for tmp in xrange(3): new_user = BRLUser("user%s" % tmp) ensure.check_grant_read_or_write_permissions_to(new_user, private_brl) bper = self.store.read_block_permissions(private_brl) bper.write.revoke(new_user) self.store.upsert_block_permissions(bper) # Try to downgrade to personal plan ensure.check_can_change_current_subscription("cooper", "personal_7_1_x") # Try to downgrade to free plan with self.assertRaisesRegexp(ForbiddenException, "You are currently using 1 users, " "reduce to 0 before plan downgrade"): ensure.check_can_change_current_subscription("cooper", "free") # Remove last collaborator bper = self.store.read_block_permissions(private_brl) bper.write.revoke("user3") self.store.upsert_block_permissions(bper) with self.assertRaisesRegexp(ForbiddenException, "You have 1 private blocks, " "reduce it to 0 before plan downgrade"): ensure.check_can_change_current_subscription("cooper", "free")
def setUp(self): testUser = BRLUser('compareUser') self.brl_block = BRLBlock('%s/%s/%s/master' % (testUser, testUser, 'modulea')) r1 = SimpleCell(self.brl_block.block_name + 'r1.h') content = Content(id_=None, load=Blob('hello')) self.r1 = r1 self.c1 = content self.last_version_resources = {'r1.h': Resource(r1, content)}
def test_simple_name(self): _ = BRLUser("testuser1") _ = BRLUser(" testuser2 ") _ = BRLUser(" TestUser3 ") _ = BRLUser("abdmvn___s") _ = BRLUser(" ficherito_33") self.assertRaises(BiiException, BRLUser, "2testuser") self.assertRaises(BiiException, BRLUser, "_testuser") self.assertRaises(BiiException, BRLUser, "22") self.assertRaises(BiiException, BRLUser, "m") self.assertRaises(BiiException, BRLUser, "m" * 51) self.assertRaises(BiiException, BRLUser, "") self.assertRaises(BiiException, BRLUser, "=jkllf89o") self.assertRaises(BiiException, BRLUser, "hola-adios") self.assertRaises(BiiException, BRLUser, "TheSimpsons!!!") self.assertRaises(BiiException, BRLUser, "aaaaa*aaaaaa") self.assertRaises(BiiException, BRLUser, "'; delete from users") self.assertRaises(BiiException, BRLUser, "\"; delete from users") self.assertRaises(BiiException, BRLUser, " 33_ficherito ")
def handleLogin(self): if (self.textName.text() and self.textPass.text()): try: self.username = BRLUser(str(self.textName.text())) self.password = str(self.textPass.text()) self.close() except InvalidNameException: QtGui.QMessageBox.warning(self, 'Error', 'Bad user name') else: QtGui.QMessageBox.warning(self, 'Error', 'Insert user name and password')
def request_login(self, username=None): """Request user to input their name and password :param username If username is specified it only request password""" user_input = '' while not username: try: self.out.input_text('Username: '******'%s is not a valid username' % user_input) self._out.input_text('Please enter a password for "%s" account: ' % username) try: pwd = getpass.getpass("") except Exception as e: raise BiiException('Cancelled pass %s' % e) return username, pwd
def __new__(cls, name, validate=True): '''user/modname/branch ie: owner/[originalowner/name]/branch ''' assert isinstance(name, basestring), '%s is not a string' % str(name) if not validate: obj = str.__new__(cls, name) else: name = name.strip().replace('\\', '/') tokens = name.split('/') if len(tokens) != 4: raise InvalidNameException( "Invalid block name: %s, it should be in the form " "owner/originalowner/name/branch" % name) user = tokens[0] block_name = '/'.join(tokens[1:3]) obj = str.__new__(cls, '%s/%s/%s' % (user, block_name, tokens[3])) obj._user = BRLUser(user) obj._block_name = BlockName(block_name) obj._branch = ComplexName(tokens[3].lower()) return obj
def test_equals(self): perm1 = ElementPermissions(1, True) perm2 = ElementPermissions(1, False) self.assertNotEqual(perm1, perm2) perm1.is_private = False perm1.read.grant(BRLUser("laso")) perm2.read.grant(BRLUser("laso")) self.assertEqual(perm1, perm2) perm1.read.grant(BRLUser("laso2")) perm2.read.grant(BRLUser("laso3")) self.assertNotEqual(perm1, perm2) perm1.read.grant(BRLUser("laso3")) perm2.read.grant(BRLUser("laso2")) self.assertEqual(perm1, perm2) perm1.write.grant(BRLUser("laso")) self.assertNotEqual(perm1, perm2) perm2.write.grant(BRLUser("laso")) self.assertEqual(perm1, perm2)
def testName2(self): with self.assertRaises(InvalidNameException): BRLUser('us&%') with self.assertRaises(InvalidNameException): BRLUser('us.')
def test_brluser_underscore(self): user = BRLUser('username_1') self.assertEqual('username_1', user)
def _parse(self): tokens = self.split('/', 1) self._user = BRLUser(tokens[0], False) self._name = ComplexName(tokens[1], False)
def test_subscription_limits(self): # Create users for test self.store.create_user(User("cooper")) self.store.create_user(User("bob")) self.store.create_user(User("lady_lug")) # Coop has a private block and a public block public_brl = BRLBlock("cooper/cooper/public_block/master") private_brl = BRLBlock("cooper/cooper/private_block/master") self._add_block_to_user("cooper", public_brl, False) self._add_block_to_user("cooper", private_brl, True) # 1 contributor and unlimited private blocks self._subscribe("cooper", "personal_7_1_x") # Add an administrator ensure = Security("cooper", self.store) ensure.check_grant_administrator_for("cooper", "bob") cooper = self.store.read_user("cooper") cooper.administrators.grant("bob") self.store.update_user(cooper) # Try to add another one, it must fail self.assertRaises(PlanUpgradeNeeded, ensure.check_grant_administrator_for, "cooper", "lady_lug") # Try to add write permissions to a public block. Its ok ensure.check_grant_read_or_write_permissions_to("lady_lug", public_brl) # Try to add write permissions to a private block, but a bob (already admin) ensure.check_grant_read_or_write_permissions_to("bob", private_brl) # Try to add write permissions to a private block. It must fail self.assertRaises(PlanUpgradeNeeded, ensure.check_grant_read_or_write_permissions_to, "lady_lug", private_brl) # Remove Adminsitrator, try to add write permissions in a private block. Its ok cooper = self.store.read_user("cooper") cooper.administrators.revoke("bob") self.store.update_user(cooper) ensure.check_grant_read_or_write_permissions_to("lady_lug", private_brl) cooper.administrators.grant("lady_lug") self.store.update_user(cooper) # Subscribe to a bigger plan and check limits self._subscribe("cooper", "startup_35_5_x") # Add 4 more for tmp in xrange(4): new_user = BRLUser("user%s" % tmp) ensure.check_grant_read_or_write_permissions_to(new_user, private_brl) bper = self.store.read_block_permissions(private_brl) bper.write.grant(new_user) self.store.upsert_block_permissions(bper) # The sixth must fail self.assertRaises(PlanUpgradeNeeded, ensure.check_grant_read_or_write_permissions_to, "new_user", private_brl) # unless user is already a contributor ensure.check_grant_read_or_write_permissions_to("lady_lug", private_brl)
def test_create_blockname(self): user = BRLUser("user2") bn = user.create_block_name("block2") self.assertIsInstance(bn, BlockName)
def testName(self): user = BRLUser('username') self.assertEqual('username', user) user2 = BRLUser('Username ') self.assertEqual('Username', user2) self.assertNotEqual(user, user2)
def _parse(self): tokens = self.split('/') self._user = BRLUser(tokens[0], False) self._block_name = BlockName('/'.join(tokens[1:3]), False) self._branch = tokens[3]
def setUp(self): test_user = BRLUser('compareUser') self.block = BRLBlock('%s/%s/%s/master' % (test_user, test_user, 'blocka')) self.service = FakeService(self.block)
def setUp(self): self.testuser = BRLUser('user') self.edition_api = TestEditionAPI() self.hive_manager = HiveManager(self.edition_api, None, OutputStream()) self.block_name = BlockName('dummy/myblock')