private static bool IsStaleRole(List<RoleRow> l, out RoleRow role) { role = null; if (l == null || l.Count == 0) { return false; } string rolename = l.ElementAt(0).RoleName; foreach (RoleRow row in l) { Debug.Assert(row.RoleName == rolename, "rolenames do not match"); if (string.IsNullOrEmpty(row.UserName)) { role = row; return false; } } return true; }
// Because of limited transactional support in the table storage offering, this function gives limited guarantees // for inserting all users into all roles. // We do not recommend using this function because of missing transactional support. public override void AddUsersToRoles(string[] usernames, string[] roleNames) { SecUtility.CheckArrayParameter(ref roleNames, true, true, true, MaxTableRoleNameLength, "roleNames"); SecUtility.CheckArrayParameter(ref usernames, true, true, true, Constants.MaxTableUsernameLength, "usernames"); RoleRow row; try { TableServiceContext svc = this.CreateDataServiceContext(); foreach (string role in roleNames) { if (!this.RoleExists(role)) { throw new ProviderException(string.Format(CultureInfo.InstalledUICulture, "The role {0} does not exist!", role)); } foreach (string user in usernames) { row = new RoleRow(this.applicationName, role, user); try { svc.AddObject(this.tableName, row); svc.SaveChangesWithRetries(); } catch (InvalidOperationException e) { if (e.InnerException is DataServiceClientException && (e.InnerException as DataServiceClientException).StatusCode == (int)HttpStatusCode.Conflict) { // this element already exists or was created in a failed retry // this is not a fatal error; continue adding elements Log.Write(EventKind.Warning, string.Format(CultureInfo.InstalledUICulture, "The user {0} already exists in the role {1}.", user, role)); svc.Detach(row); } else { throw new ProviderException(string.Format(CultureInfo.InstalledUICulture, "Error adding user {0} to role {1}", user, role)); } } } } } catch (InvalidOperationException e) { throw new ProviderException("Error while accessing the data store.", e); } }
public override void CreateRole(string roleName) { SecUtility.CheckParameter(ref roleName, true, true, true, MaxTableRoleNameLength, "rolename"); try { TableServiceContext svc = this.CreateDataServiceContext(); RoleRow newRole = new RoleRow(this.applicationName, roleName, string.Empty); svc.AddObject(this.tableName, newRole); svc.SaveChangesWithRetries(); } catch (InvalidOperationException e) { // when retry policies are used we cannot distinguish between a conflict and success // so, in the case of a conflict, we just retrun success here if (e.InnerException is DataServiceClientException && (e.InnerException as DataServiceClientException).StatusCode == (int)HttpStatusCode.Conflict) { // the role already exists return; } throw new ProviderException("Error accessing role table.", e); } }