/// <summary>
        /// Remove a role from a user. If the role does not exist
        /// or the user doesn't havit, nothing happens.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="roleName">The name of the role to remove from the user.</param>
        /// <returns>Returns a task for the operation.</returns>
        public virtual async Task RemoveFromRoleAsync(IdentityUser <U> user, string roleName)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (roleName == null)
            {
                throw new ArgumentNullException(nameof(roleName));
            }

            var domainUser = user.DomainUser;

            using (var transaction = DomainContainer.BeginTransaction())
            {
                DomainContainer.Users.Attach(domainUser);

                var removedRole = domainUser.Roles.Where(r => r.Name == roleName).FirstOrDefault();

                if (removedRole == null)
                {
                    return;
                }

                domainUser.Roles.Add(removedRole);

                await DomainContainer.SaveChangesAsync();

                transaction.Commit();
            }
        }
Esempio n. 2
0
 public bool Exist()
 {
     using (var context = new DomainContainer())
     {
         return(context.Database.Exists());
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Add a <see cref="Registration"/> to a user of type <typeparamref name="U"/>.
        /// </summary>
        /// <param name="user">
        /// The user.
        /// </param>
        /// <param name="cancellationToken">Cancellation token for the operation.</param>
        /// <param name="login">
        /// The <see cref="UserLoginInfo"/>
        /// that corresponds to the <see cref="Registration"/>.
        /// </param>
        /// <returns>Returns the task which completes the operation.</returns>
        public virtual async Task AddLoginAsync(U user, UserLoginInfo login, CancellationToken cancellationToken)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (login == null)
            {
                throw new ArgumentNullException(nameof(login));
            }

            using (var transaction = DomainContainer.BeginTransaction())
            {
                var registration = DomainContainer.Registrations.Create();

                RegistrationProvider registrationProvider;

                registrationProvider = GetRegistrationProvider(login);

                registration.Provider    = registrationProvider;
                registration.ProviderKey = login.ProviderKey;
                registration.User        = user;

                await OnAddingLoginAsync(registration);

                DomainContainer.Registrations.Add(registration);

                await DomainContainer.SaveChangesAsync(cancellationToken);

                transaction.Commit();
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Remove an external login of a user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="loginProvider">The login provider name.</param>
        /// <param name="providerKey">The login provider key.</param>
        /// <param name="cancellationToken">Cancellation token for the operation.</param>
        /// <returns>Returns a task for the operation.</returns>
        public virtual async Task RemoveLoginAsync(U user, string loginProvider, string providerKey, CancellationToken cancellationToken)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (loginProvider == null)
            {
                throw new ArgumentNullException(nameof(loginProvider));
            }
            if (providerKey == null)
            {
                throw new ArgumentNullException(nameof(providerKey));
            }

            var registrationProvider = GetRegistrationProvider(loginProvider);

            using (var transaction = DomainContainer.BeginTransaction())
            {
                var registrationFound =
                    user.Registrations.FirstOrDefault(registration =>
                                                      registration.ProviderKey == providerKey && registration.Provider == registrationProvider);

                if (registrationFound != null)
                {
                    await OnRemovingLoginAsync(registrationFound);

                    this.DomainContainer.Registrations.Remove(registrationFound);

                    await DomainContainer.SaveChangesAsync();
                }

                transaction.Commit();
            }
        }
        /// <summary>
        /// Remove an external login of a user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="login">The representation of an external login.</param>
        /// <returns>Returns a task for the operation.</returns>
        public virtual async Task RemoveLoginAsync(IdentityUser <U> user, UserLoginInfo login)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (login == null)
            {
                throw new ArgumentNullException(nameof(login));
            }

            var registrationProvider = GetRegistrationProvider(login);

            using (var transaction = DomainContainer.BeginTransaction())
            {
                var registrationFound =
                    user.DomainUser.Registrations.FirstOrDefault(registration =>
                                                                 registration.ProviderKey == login.ProviderKey && registration.Provider == registrationProvider);

                if (registrationFound != null)
                {
                    await OnRemovingLoginAsync(registrationFound);

                    this.DomainContainer.Registrations.Remove(registrationFound);

                    await DomainContainer.SaveChangesAsync();
                }

                transaction.Commit();
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Add a role to a user. The role must exist in the system.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="roleName">The name of the role to add.</param>
        /// <param name="cancellationToken">Cancellation token for the action.</param>
        /// <returns>Returns a task completing the operation.</returns>
        /// <exception cref="IdentityException">
        /// Thrown when a role having the given <paramref name="roleName"/>
        /// does not exist in the system.
        /// </exception>
        public virtual async Task AddToRoleAsync(U user, string roleName, CancellationToken cancellationToken)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (roleName == null)
            {
                throw new ArgumentNullException(nameof(roleName));
            }

            using (var transaction = DomainContainer.BeginTransaction())
            {
                this.DomainContainer.Users.Attach(user);

                if (user.Roles.Any(r => r.Name == roleName))
                {
                    return;
                }

                var role = await FindRoleAsync(roleName, cancellationToken);

                if (role == null)
                {
                    throw new IdentityException($"The role '{roleName}' does not exist in the system.");
                }

                user.Roles.Add(role);

                await this.DomainContainer.SaveChangesAsync(cancellationToken);

                transaction.Commit();
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Remove a role from a user. If the role does not exist
        /// or the user doesn't havit, nothing happens.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="roleName">The name of the role to remove from the user.</param>
        /// <param name="cancellationToken">Cancellation token for the action.</param>
        /// <returns>Returns a task for the operation.</returns>
        public virtual async Task RemoveFromRoleAsync(U user, string roleName, CancellationToken cancellationToken)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (roleName == null)
            {
                throw new ArgumentNullException(nameof(roleName));
            }

            using (var transaction = DomainContainer.BeginTransaction())
            {
                this.DomainContainer.Users.Attach(user);

                var removedRole = user.Roles.Where(r => r.Name == roleName).FirstOrDefault();

                if (removedRole == null)
                {
                    transaction.Pass();

                    return;
                }

                user.Roles.Remove(removedRole);

                await this.DomainContainer.SaveChangesAsync(cancellationToken);

                transaction.Commit();
            }
        }
Esempio n. 8
0
 public void Create()
 {
     using (var context = new DomainContainer())
     {
         context.Database.Create();
     }
 }
Esempio n. 9
0
        /// <summary>
        /// Create a user.
        /// </summary>
        /// <param name="user">The user to create.</param>
        /// <param name="cancellationToken">Cancellation token used during saving.</param>
        /// <returns>Returns the task which completes the operation.</returns>
        /// <remarks>
        /// <see cref="OnCreatingUserAsync"/> is invoked whose default implementation
        /// fires the <see cref="CreatingUser"/> event during this method.
        /// </remarks>
        public virtual async Task <IdentityResult> CreateAsync(U user, CancellationToken cancellationToken)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            using (var transaction = DomainContainer.BeginTransaction())
            {
                this.DomainContainer.Users.Add(user);

                user.SecurityStamp = String.Empty;
                user.CreationDate  = DateTime.UtcNow;
                if (user.Guid == default)
                {
                    user.Guid = Guid.NewGuid();
                }

                await OnCreatingUserAsync(user);

                await this.DomainContainer.SaveChangesAsync(cancellationToken);

                transaction.Commit();
            }

            return(IdentityResult.Success);
        }
Esempio n. 10
0
 public void Drop()
 {
     using (var context = new DomainContainer())
     {
         context.Database.Delete();
     }
 }
Esempio n. 11
0
        protected override void OnStart(string[] args)
        {
            Log4NetEventLogImpl log4NetEventLogImpl = new Log4NetEventLogImpl();

            log4NetEventLogImpl.Info("Glue4Net Server Copyright @ henryfan 2014 Version " + typeof(Glue4NetService).Assembly.GetName().Version);
            log4NetEventLogImpl.Info("Website:http://www.ikende.com");
            log4NetEventLogImpl.Info("Email:[email protected]");
            this.mContainer     = new DomainContainer();
            this.mContainer.Log = new Log4NetEventLogImpl();
            this.mContainer.LoadConfig("containerSection");
            this.mContainer.Open();
        }
Esempio n. 12
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddDbContext <PKShopContext>(options =>
                                                  options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddDbContext <EventStoreContext>(options =>
                                                      options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity <ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(o =>
            {
                o.LoginPath        = new PathString("/login");
                o.AccessDeniedPath = new PathString("/home/access-denied");
            })
            .AddFacebook(o =>
            {
                o.AppId     = Configuration["Authentication:Facebook:AppId"];
                o.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
            })
            .AddGoogle(googleOptions =>
            {
                googleOptions.ClientId     = Configuration["Authentication:Google:ClientId"];
                googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
            });

            services.AddMvc();
            services.AddScoped <ServiceFactory>(x => x.GetService);

            services.AddMediatR(typeof(Startup));
            services.AddAutoMapperSetup();

            var builder = new ContainerBuilder();

            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
            .AsImplementedInterfaces();

            DomainContainer.Load(builder);
            BusContainer.Load(builder);
            IndentityContainer.Load(builder);
            ServicesContainer.Load(builder);
            DataContainer.Load(builder);
            builder.Populate(services);
            Container = builder.Build();

            return(new AutofacServiceProvider(Container));
        }
Esempio n. 13
0
        private static void Main(string[] args)
        {
            Log4NetEventLogImpl log4NetEventLogImpl = new Log4NetEventLogImpl();

            log4NetEventLogImpl.Info("Glue4Net Console Copyright @ henryfan 2014 Version " + typeof(Program).Assembly.GetName().Version);
            log4NetEventLogImpl.Info("Website:http://www.ikende.com");
            log4NetEventLogImpl.Info("Email:[email protected]");
            mContainer     = new DomainContainer();
            mContainer.Log = log4NetEventLogImpl;
            mContainer.LoadConfig("containerSection");
            mContainer.Open();
            Thread.Sleep(-1);
        }
Esempio n. 14
0
        /// <summary>
        /// Set the e-mail of a user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="email">the user's e-mail.</param>
        /// <param name="cancellationToken">Cancellation token for the operation.</param>
        /// <returns>Returns a task which completes the operation.</returns>
        public virtual async Task SetEmailAsync(U user, string email, CancellationToken cancellationToken)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (email == null)
            {
                throw new ArgumentNullException(nameof(email));
            }

            using (var transaction = DomainContainer.BeginTransaction())
            {
                user.Email = email;

                await OnSettingEmailAsync(user);

                await DomainContainer.SaveChangesAsync(cancellationToken);

                transaction.Commit();
            }
        }
        /// <summary>
        /// Delete a user.
        /// </summary>
        /// <param name="user">The user to create.</param>
        /// <returns>Returns the task which completes the operation.</returns>
        /// <remarks>
        /// <see cref="OnDeletingUserAsync"/> is invoked whose default implementation
        /// fires the <see cref="DeletingUser"/> event during this method.
        /// </remarks>
        public virtual async Task DeleteAsync(IdentityUser <U> user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            var domainUser = user.DomainUser;

            using (var transaction = DomainContainer.BeginTransaction())
            {
                DomainContainer.Users.Attach(domainUser);

                await OnDeletingUserAsync(domainUser);

                DomainContainer.Users.Remove(domainUser);

                await DomainContainer.SaveChangesAsync();

                transaction.Commit();
            }
        }
        /// <summary>
        /// Update a user.
        /// </summary>
        /// <param name="user"></param>
        /// <returns>Returns the task which completes the operation.</returns>
        /// <remarks>
        /// <see cref="OnUpdatingUserAsync"/> is invoked whose default implementation
        /// fires the <see cref="DeletingUser"/> event during this method.
        /// </remarks>
        public virtual async Task UpdateAsync(IdentityUser <U> user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var domainUser = user.DomainUser;

            using (var transaction = DomainContainer.BeginTransaction())
            {
                DomainContainer.Users.Attach(domainUser);

                await OnUpdatingUserAsync(domainUser);

                DomainContainer.SetAsModified(domainUser);

                await DomainContainer.SaveChangesAsync();

                transaction.Commit();
            }
        }
Esempio n. 17
0
        /// <summary>
        /// Update a user.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="cancellationToken">Cancellation token used during saving.</param>
        /// <returns>Returns the task which completes the operation.</returns>
        /// <remarks>
        /// <see cref="OnUpdatingUserAsync"/> is invoked whose default implementation
        /// fires the <see cref="DeletingUser"/> event during this method.
        /// </remarks>
        public virtual async Task <IdentityResult> UpdateAsync(U user, CancellationToken cancellationToken)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            using (var transaction = DomainContainer.BeginTransaction())
            {
                DomainContainer.Users.Attach(user);

                await OnUpdatingUserAsync(user);

                DomainContainer.SetAsModified(user);

                await DomainContainer.SaveChangesAsync(cancellationToken);

                transaction.Commit();
            }

            return(IdentityResult.Success);
        }
        /// <summary>
        /// Set whether a user's e-mail is confirmed.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="confirmed">True if the user's e-mail is confirmed.</param>
        /// <returns>Returns a task which completes the operation.</returns>
        public virtual async Task SetEmailConfirmedAsync(IdentityUser <U> user, bool confirmed)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            using (var transaction = DomainContainer.BeginTransaction())
            {
                var domainUser = user.DomainUser;

                if (domainUser.RegistrationStatus == RegistrationStatus.PendingVerification)
                {
                    domainUser.RegistrationStatus = RegistrationStatus.Verified;

                    await OnConfirmingEmailAsync(domainUser);

                    await DomainContainer.SaveChangesAsync();

                    transaction.Commit();
                }
            }
        }
        /// <summary>
        /// Create a user.
        /// </summary>
        /// <param name="user">The user to create.</param>
        /// <returns>Returns the task which completes the operation.</returns>
        /// <remarks>
        /// <see cref="OnCreatingUserAsync"/> is invoked whose default implementation
        /// fires the <see cref="CreatingUser"/> event during this method.
        /// </remarks>
        public virtual async Task CreateAsync(IdentityUser <U> user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var domainUser = user.DomainUser;

            using (var transaction = DomainContainer.BeginTransaction())
            {
                DomainContainer.Users.Add(domainUser);

                domainUser.SecurityStamp = String.Empty;
                domainUser.CreationDate  = DateTime.UtcNow;
                domainUser.Guid          = new Guid();

                await OnCreatingUserAsync(domainUser);

                await DomainContainer.SaveChangesAsync();

                transaction.Commit();
            }
        }
Esempio n. 20
0
 public UnitOfWork()
 {
     this.dataContext = new DomainContainer();
     this.dataContext.Configuration.AutoDetectChangesEnabled = false;
     this.ContactRepository = new ContactRepository(dataContext);
 }
Esempio n. 21
0
        protected override void Execute(CodeActivityContext context)
        {
            PrincipalContext principalContext;

            if (IsLocalAccount.Get(context))
            {
                principalContext = new PrincipalContext(ContextType.Machine);
            }
            else
            {
                principalContext = new PrincipalContext(ContextType.Domain, Domain.Get(context), DomainContainer.Get(context));
            }

            UserPrincipal principal = UserPrincipal.FindByIdentity(principalContext, Username.Get(context));

            if (principal == null)
            {
                principal                      = new UserPrincipal(principalContext, Username.Get(context), Password.Get(context), true);
                principal.DisplayName          = Firstname.Get(context) + " " + Lastname.Get(context);
                principal.PasswordNeverExpires = true;
                principal.Save();

                IList <string> groups = GroupMemberships.Get(context) ?? new List <string>();

                foreach (var group in groups)
                {
                    GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, group);
                    if (!groupPrincipal.Members.Contains(principal))
                    {
                        groupPrincipal.Members.Add(principal);
                    }
                    groupPrincipal.Save();
                }
            }
            else if (UpdateExistingUser.Get(context))
            {
                principal.SetPassword(Password.Get(context));
                principal.DisplayName          = Firstname.Get(context) + " " + Lastname.Get(context);
                principal.PasswordNeverExpires = true;
                principal.Save();

                IList <string> groups = GroupMemberships.Get(context) ?? new List <string>();

                foreach (var group in groups)
                {
                    GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, group);
                    if (!groupPrincipal.Members.Contains(principal))
                    {
                        groupPrincipal.Members.Add(principal);
                    }
                    groupPrincipal.Save();
                }

                GroupPrincipal allGroups = new GroupPrincipal(principalContext);
                allGroups.Name = "*";
                PrincipalSearcher searcher = new PrincipalSearcher(allGroups);
                var allGroupList           = searcher.FindAll();
                foreach (GroupPrincipal group in allGroupList)
                {
                    if (!groups.Contains(group.Name) && group.Members.Contains(principal))
                    {
                        group.Members.Remove(principal);
                        group.Save();
                    }
                }
            }
        }