コード例 #1
0
        public async Task UpdateRoleAsync(CassandraRole role, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (role == null)
            {
                throw new ArgumentNullException("role");
            }
            if (string.IsNullOrEmpty(role.Name))
            {
                throw new ArgumentNullException("role", "role.Name cannot be null or empty");
            }

            var modifiedDate = DateTimeOffset.UtcNow;

            role.Modified = modifiedDate;
            var    batch = new BatchStatement();
            string oldName;
            var    changed = role.HasNameChanged(out oldName);

            if (changed)
            {
                // This a Create/Delete move
                // can only change the name if that target name does not exist.
                var findResult = await FindRoleByNameAsync(role.Name);

                if (findResult.Any())
                {
                    // sorry, no go, this is record must either be deleted first or use another name.
                    throw new Exception(
                              string.Format(
                                  "Cannot change role name:[{0}] to an existing role, pick another name, or delete this one first",
                                  role.Name));
                }
                var boundStatements = await BuildCreateStatements(role);

                batch.AddRange(boundStatements);
                var oldRole = new CassandraRole(role.TenantId, oldName);
                boundStatements = await BuildDeleteStatements(oldRole);

                batch.AddRange(boundStatements);
                await CassandraSession.ExecuteAsync(batch).ConfigureAwait(false);
                await RenameRoleNameInUsersAsync(oldName, role.Name, cancellationToken);
            }
            else
            {
                PreparedStatement[] prepared = await _updateRole;

                // UPDATE roles ...
                batch.Add(prepared[0].Bind(role.Name, role.DisplayName, role.IsSystemRole, modifiedDate, role.Id));
                // UPDATE roles_by_name ... (since username hasn't changed)
                batch.Add(prepared[1].Bind(role.DisplayName, role.IsSystemRole, modifiedDate, role.TenantId, role.Name));

                await CassandraSession.ExecuteAsync(batch).ConfigureAwait(false);
            }
        }
コード例 #2
0
        public async Task DeleteRoleAsync(CassandraRole role)
        {
            PreparedStatement[] prepared = await _deleteRole;
            var batch = new BatchStatement();

            // DELETE FROM roles ...
            batch.Add(prepared[0].Bind(role.Id));

            // DELETE FROM roles_by_rolename ...
            batch.Add(prepared[1].Bind(role.Name, role.TenantId));
            await CassandraSession.ExecuteAsync(batch).ConfigureAwait(false);
        }
コード例 #3
0
        private async Task <IEnumerable <BoundStatement> > BuildDeleteStatements(CassandraRole role)
        {
            List <BoundStatement> boundStatements = new List <BoundStatement>();

            PreparedStatement[] prepared = await _deleteRole;

            // DELETE FROM roles ...
            boundStatements.Add(prepared[0].Bind(role.Id));

            // DELETE FROM roles_by_rolename ...
            boundStatements.Add(prepared[1].Bind(role.Name, role.TenantId));
            return(boundStatements);
        }
コード例 #4
0
        public async Task CreateRoleAsync(CassandraRole role,
                                          CancellationToken cancellationToken = default(CancellationToken))
        {
            if (role == null)
            {
                throw new ArgumentNullException("role");
            }
            var boundStatements = await BuildCreateStatements(role);

            cancellationToken.ThrowIfCancellationRequested();

            var batch = new BatchStatement();

            batch.AddRange(boundStatements);

            await CassandraSession.ExecuteAsync(batch).ConfigureAwait(false);
        }
コード例 #5
0
        private async Task <IEnumerable <BoundStatement> > BuildCreateStatements(CassandraRole role)
        {
            var createdDate = DateTimeOffset.UtcNow;

            role.Created  = createdDate;
            role.TenantId = role.IsGlobal ? Guid.Empty : TenantId;

            PreparedStatement[]   preparedStatements = await _createRole;
            List <BoundStatement> boundStatements    = new List <BoundStatement>();
            var batch = new BatchStatement();

            foreach (var preparedStatement in preparedStatements)
            {
                boundStatements.Add(preparedStatement.Bind(role.Id, role.Name, role.DisplayName, role.IsSystemRole, role.IsGlobal,
                                                           role.TenantId, createdDate, null));
            }
            return(boundStatements);
        }
コード例 #6
0
        /// <summary>
        /// Creates a CassandraUser from a Row.  If the Row is null, returns null.
        /// </summary>
        public static CassandraRole FromRow(this Row row)
        {
            if (row == null)
            {
                return(null);
            }

            var role = new CassandraRole()
            {
                Name         = row.GetValue <string>("name"),
                DisplayName  = row.GetValue <string>("displayname"),
                IsSystemRole = row.GetValue <bool>("is_systemrole"),
                IsGlobal     = row.GetValue <bool>("is_global"),
                TenantId     = row.GetValue <Guid>("tenantid"),
                Created      = row.GetValue <DateTimeOffset>("created"),
                Modified     = row.IsNull("modified") ? new DateTimeOffset?() : row.GetValue <DateTimeOffset>("modified"),
            };

            return(role);
        }