示例#1
0
            public async Task CreateAsync(CassandraRole role)
            {
                if (role == null)
                {
                    throw new ArgumentNullException("role");
                }

                var createdDate = DateTimeOffset.UtcNow;

                role.Created  = createdDate;
                role.TenantId = role.IsGlobal ? new Guid() : _tenantId;

                PreparedStatement[] prepared = await _createRole;

                var batch = new BatchStatement();

                //INSERT INTO roles...
                batch.Add(prepared[0].Bind(role.Id, role.Name, role.DisplayName, role.IsSystemRole, role.IsGlobal,
                                           role.TenantId, createdDate, null));

                //INSERT INTO roles_by_rolename...
                batch.Add(prepared[1].Bind(role.Name, role.TenantId, role.Id, role.DisplayName, role.IsSystemRole,
                                           role.IsGlobal, createdDate, null));
                await _session.ExecuteAsync(batch).ConfigureAwait(false);
            }
示例#2
0
            public async Task <IQueryable <CassandraRole> > GetAllRoles()
            {
                PreparedStatement prepared = await _find;
                BoundStatement    bound    = prepared.Bind(_tenantId);
                RowSet            rows     = await _session.ExecuteAsync(bound).ConfigureAwait(false);

                return(rows.Select(r => CassandraRole.FromRow(r)).ToArray().AsQueryable());
            }
示例#3
0
            public async Task <CassandraRole> FindByNameAsync(string roleName)
            {
                PreparedStatement prepared = await _findByName;
                BoundStatement    bound    = prepared.Bind(roleName, _tenantId);

                RowSet rows = await _session.ExecuteAsync(bound).ConfigureAwait(false);

                return(CassandraRole.FromRow(rows.SingleOrDefault()));
            }
示例#4
0
            public async Task <CassandraRole> FindByIdAsync(Guid roleId)
            {
                PreparedStatement prepared = await _findById;
                BoundStatement    bound    = prepared.Bind(roleId);

                RowSet rows = await _session.ExecuteAsync(bound).ConfigureAwait(false);

                var role = CassandraRole.FromRow(rows.SingleOrDefault());

                return(role != null && (role.IsGlobal || role.TenantId == _tenantId) ? role : null);
            }
示例#5
0
            public async Task DeleteAsync(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 _session.ExecuteAsync(batch).ConfigureAwait(false);
            }
示例#6
0
            public async Task UpdateAsync(CassandraRole role)
            {
                if (role == null)
                {
                    throw new ArgumentNullException("role");
                }

                var modifiedDate = DateTimeOffset.UtcNow;

                role.Modified = modifiedDate;

                PreparedStatement[] prepared = await _updateRole;
                var batch = new BatchStatement();

                // UPDATE roles ...
                batch.Add(prepared[0].Bind(role.Name, role.DisplayName, role.IsSystemRole, modifiedDate, role.Id));

                // See if the name changed we can decide whether we need a different roles_by_name record
                string oldName;

                if (role.HasNameChanged(out oldName) == false)
                {
                    // UPDATE roles_by_name ... (since username hasn't changed)
                    batch.Add(prepared[1].Bind(role.DisplayName, role.IsSystemRole, modifiedDate, role.TenantId, oldName));
                }
                else
                {
                    // DELETE FROM roles_by_name ... (delete old record since name changed)
                    if (string.IsNullOrEmpty(oldName) == false)
                    {
                        batch.Add(prepared[2].Bind(oldName, role.TenantId));
                    }

                    // INSERT INTO roles_by_name ... (insert new record since name changed)
                    if (string.IsNullOrEmpty(role.Name) == false)
                    {
                        batch.Add(prepared[3].Bind(role.Name, role.TenantId, role.Id, role.DisplayName, role.IsSystemRole,
                                                   role.IsGlobal, role.Created, modifiedDate));
                    }
                }

                await _session.ExecuteAsync(batch).ConfigureAwait(false);
            }
        /// <summary>
        /// Creates a CassandraUser from a Row.  If the Row is null, returns null.
        /// </summary>
        internal static CassandraRole FromRow(Row row)
        {
            if (row == null)
            {
                return(null);
            }

            var role = new CassandraRole(row.GetValue <string>("name"), row.GetValue <Guid>("roleid"))
            {
                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);
        }