コード例 #1
0
        /// <summary>
        /// Deletes the user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="token">The token.</param>
        /// <param name="context">The context.</param>
        /// <returns>System.Boolean; true if successful, false otherwise.</returns>
        private static bool DeleteUser(ZentityUser user, AuthenticatedToken token, ZentityContext context)
        {
            if (!DataAccess.IsAdmin(token.IdentityName, context))
            {
                throw new UnauthorizedAccessException(ConstantStrings.UnauthorizedAccessException);
            }

            Identity identity = GetIdentity(user.LogOnName, context);

            if (identity == null)
            {
                throw new ArgumentException(string.Format(
                                                CultureInfo.CurrentUICulture,
                                                ConstantStrings.IdentityDoesNotExist,
                                                identity.IdentityName));
            }

            // Remove relationships
            identity.RelationshipsAsObject.Load();
            identity.RelationshipsAsSubject.Load();
            foreach (Relationship relationship in identity.RelationshipsAsObject.Union(
                         identity.RelationshipsAsSubject).ToList())
            {
                context.DeleteObject(relationship);
            }

            // Remove identity
            context.DeleteObject(identity);

            if (context.SaveChanges() == 0)
            {
                return(false);
            }

            if (!user.Unregister())
            {
                return(false);
            }

            return(true);
        }