/// <summary> /// Save role business object. /// It does create/update based on roleObject.Id. If id is empty, the method will create a new role object. /// If the specified id is invalid, the method will throw an exception. /// </summary> /// <param name="roleObject">role object</param> /// <exception cref="ValidationException">Role name existed in system</exception> /// <exception cref="ArgumentException">Role id is invalid</exception> public void Save(RoleObject roleObject) { Kit.NotNull(roleObject, "roleObject"); using (MembershipDataContext ctx = DataContextFactory.Create<MembershipDataContext>()) { try { Role role = null; using (ValidationScope validationScope = new ValidationScope(true)) { var duplicateRoleNameCount = (from r in ctx.Roles where r.ApplicationId == this.authenticationContext.ApplicationId && r.RoleName == roleObject.RoleName && r.Domain == roleObject.Domain && r.RoleId != roleObject.RoleId select r).Count(); if (duplicateRoleNameCount > 0) validationScope.Error(Resources.ExistedRoleName, roleObject.RoleName); if (roleObject.RoleId == Guid.Empty) { role = new Role() { ApplicationId = this.authenticationContext.ApplicationId, }; ctx.Roles.InsertOnSubmit(role); } else { role = ctx.Roles.FirstOrDefault(r => r.RoleId == roleObject.RoleId); if (role == null) validationScope.Error(Resources.InvalidRoleID, "roleObject"); base.RemoveCache(FormatRoleNameCacheKey(role.RoleName)); base.RemoveCache(roleObject.RoleId); } } role.Domain = roleObject.Domain; role.RoleName = roleObject.RoleName; role.LoweredRoleName = roleObject.RoleName.ToLowerInvariant(); role.Description = roleObject.Description; role.LastUpdatedDate = DateTime.UtcNow; role.Predefined = roleObject.Predefined; ctx.SubmitChanges(); roleObject.RoleId = role.RoleId; } catch (ArgumentException) { throw; } catch (ValidationException) { throw; } catch (Exception exp) { Logger.Instance(this).Error(exp); throw; } } }
/// <summary> /// Create a role into specified application. /// </summary> /// <param name="applicationId"></param> /// <param name="roleObject"></param> protected static void CreateRole(Guid applicationId, RoleObject roleObject) { Kit.NotNull(roleObject, "roleObject"); try { using (MembershipDataContext ctx = DataContextFactory.Create<MembershipDataContext>()) { Role role = new Role() { ApplicationId = applicationId, RoleName = roleObject.RoleName, LoweredRoleName = roleObject.RoleName.ToLowerInvariant(), Domain = roleObject.Domain, Description = roleObject.Description, LastUpdatedDate = DateTime.UtcNow, Predefined = true }; ctx.Roles.InsertOnSubmit(role); ctx.SubmitChanges(); roleObject.RoleId = role.RoleId; } } catch (ValidationException) { throw; } catch (Exception exp) { Logger.Instance(typeof(CredentialInstaller)).Error(exp); throw; } }
partial void UpdateRole(Role instance);
partial void DeleteRole(Role instance);
partial void InsertRole(Role instance);