private void AddOrRemoveUserToRoles(UserToRoles addOrRemove) { try { string username = allUsersInUserToRole_comboBox.SelectedItem.ToString(); string[] checkedRolesArray = RolesHelper.GetAllCheckedItemsArray(this.userToRole_groupBox); string checkedRolesString = RolesHelper.GetAllCheckedItems(this.userToRole_groupBox); switch (addOrRemove) { case UserToRoles.Add: { try { Roles.AddUserToRoles(username, checkedRolesArray); SampleMessageBox.Show(String.Format("Successfully add {0} to {1} role(s)", username, checkedRolesString)); } catch (ProviderException) { if (checkedRolesArray.Length == 1) { SampleMessageBox.Show(String.Format("{0} is already in {1} role", username, checkedRolesString)); } else { SampleMessageBox.Show(String.Format("{0} is already in some of these roles:\n{1} ", username, checkedRolesString)); } } catch (ArgumentException) { SampleMessageBox.Show("Please check some of roles"); } break; } case UserToRoles.Remove: { try { Roles.RemoveUserFromRoles(username, checkedRolesArray); SampleMessageBox.Show(String.Format("Successfully remove {0} from {1} role(s)", username, checkedRolesString)); } catch (ArgumentException) { SampleMessageBox.Show("Please check some of roles"); } break; } } DisplayUsers(Display.FirstPage); RolesHelper.UncheckedAllCheckboxes(this.userToRole_groupBox); allUsersInUserToRole_comboBox.SelectedIndex = -1; } catch (NullReferenceException) { SampleMessageBox.Show("Please, select user!"); } }
public async Task <List <UserToRoles> > GetUserToRolesItemListByNonKeyAsync(string propertyName, Object value) { var tableName = "UserToRoles"; Dictionary <string, Condition> dict = new Dictionary <string, Condition>(); AttributeValue attributeValue = null; if (value.GetType() == typeof(string)) { attributeValue = new AttributeValue { S = value.ToString() }; } if (value.GetType() == typeof(int)) { attributeValue = new AttributeValue { N = value.ToString() }; } Condition condition = new Condition { ComparisonOperator = "EQ", AttributeValueList = new List <AttributeValue> { attributeValue } }; dict.Add(propertyName, condition); var result = await _client.ScanAsync(tableName, dict); List <UserToRoles> userRoles = new List <UserToRoles>(); List <Dictionary <string, AttributeValue> > items = result.Items; foreach (Dictionary <string, AttributeValue> item in items) { int roleIdHolder = 0; foreach (var keyValuePair in item) { if (keyValuePair.Key == "RoleId") { roleIdHolder = Convert.ToInt32(keyValuePair.Value.N); } } UserToRoles userRole = new UserToRoles() { NormalizedUserName = item["NormalizedUserName"].S, RoleId = roleIdHolder }; userRoles.Add(userRole); } return(userRoles); }
public async Task AddToRoleAsync(ApplicationUser user, string roleName, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var normalizedUserName = user.NormalizedUserName.ToUpper(); var normalizedRole = roleName.ToUpper(); bool alreadyHasRole = await IsInRoleAsync(user, normalizedRole, cancellationToken); if (!alreadyHasRole) { ApplicationRole role = await _roleStore.FindByNameAsync(normalizedRole, cancellationToken); int roleId = role.RoleId; DynamoDBContext context = new DynamoDBContext(_client); UserToRoles userRole = new UserToRoles(); userRole.RoleId = role.RoleId; userRole.NormalizedUserName = normalizedUserName; var userDoc = context.ToDocument <UserToRoles>(userRole); Table table = Table.LoadTable(_client, "UserToRoles"); await table.PutItemAsync(userDoc); } return; }
public async Task <bool> UserHasRoleAsync(string normalizedUserName, int roleId) { AttributeValue hashKey = new AttributeValue { S = normalizedUserName }; Condition condition = new Condition { ComparisonOperator = "EQ", AttributeValueList = new List <AttributeValue> { new AttributeValue { N = roleId.ToString() } } }; Dictionary <string, Condition> keyConditions = new Dictionary <string, Condition> { { "NormalizedUserName", new Condition { ComparisonOperator = "EQ", AttributeValueList = new List <AttributeValue> { hashKey } } }, { "RoleId", condition } }; List <UserToRoles> userRoles = new List <UserToRoles>(); Dictionary <string, AttributeValue> startKey = null; do { QueryRequest request = new QueryRequest { TableName = "UserToRoles", ExclusiveStartKey = startKey, KeyConditions = keyConditions }; var result = await _client.QueryAsync(request); List <Dictionary <string, AttributeValue> > items = result.Items; foreach (Dictionary <string, AttributeValue> item in items) { int roleIdHolder = 0; foreach (var keyValuePair in item) { if (keyValuePair.Key == "RoleId") { roleIdHolder = Convert.ToInt32(keyValuePair.Value.N); } } UserToRoles userRole = new UserToRoles(); userRole.NormalizedUserName = item["NormalizedUserName"].S; userRole.RoleId = roleIdHolder; userRoles.Add(userRole); } startKey = result.LastEvaluatedKey; } while (startKey != null && startKey.Count > 0); return(userRoles.Count > 0); }