Exemple #1
0
        public async Task AfterSave(Int64 UserId, Object beforeData, Object afterData)
        {
            var before = beforeData as ExpandoObject;
            var after  = afterData as ExpandoObject;

            var    userId      = after.Eval <Int64>("User.Id");
            var    pwd         = before.Eval <String>("User.Password");
            String tenantRoles = null;

            if (_host.IsMultiTenant)
            {
                tenantRoles = before.Eval <String>("User.TenantRoles");
                var afterUser = after.Get <ExpandoObject>("User");
                afterUser.Set("TenantRoles", tenantRoles);
            }

            var token = await _userManager.GeneratePasswordResetTokenAsync(userId);

            var ir = await _userManager.ResetPasswordAsync(userId, token, pwd);

            if (!ir.Succeeded)
            {
                String errors = String.Empty;
                foreach (var e in ir.Errors)
                {
                    errors += "\n" + e;
                }
                throw new SecurityException("Set password failed." + errors);
            }
            var user = await _userManager.FindByIdAsync(userId);

            user.EmailConfirmed = true;
            if (tenantRoles != null)
            {
                user.TenantRoles = tenantRoles;
            }
            user.SetModified(UserModifiedFlag.EmailConfirmed);
            await _userManager.UpdateAsync(user);

            if (_host.IsMultiTenant && _host.IsMultiCompany)
            {
                var update = new UpdateTenantCompanyHandler();
                update.Inject(_host, _dbContext);
                update.EnableThrow();
                update.DisableDtc();
                update.Invoke(UserId, 0);
            }
        }
Exemple #2
0
        public Object Invoke(Int64 UserId, Int64 Id)
        {
            if (!_host.IsMultiTenant)
            {
                throw new InvalidOperationException("DeleteTenantUser is available only in multitenant environment");
            }
            var prms = new DeleteTenantUserParams()
            {
                UserId   = UserId,
                Id       = Id,
                TenantId = _host.TenantId ?? 0
            };
            var appUser = new AppUser()
            {
                Id          = Id,
                Tenant      = _host.TenantId ?? 0,
                CurrentUser = UserId
            };
            var result = new DeleteTeanantUserResult();

            void ExecuteSql()
            {
                _dbContext.Execute <DeleteTenantUserParams>(_host.TenantDataSource, "a2security_tenant.[DeleteUser]", prms);
                _dbContext.Execute <DeleteTenantUserParams>(_host.CatalogDataSource, "a2security.[DeleteTenantUser]", prms);
                //await _userManager.DeleteAsync(appUser);

                if (_host.IsMultiCompany && _host.IsMultiTenant)
                {
                    var update = new UpdateTenantCompanyHandler();
                    update.Inject(_host, _dbContext);
                    update.EnableThrow();
                    update.DisableDtc();
                    update.Invoke(UserId, 0);
                }
            }

            try
            {
                if (_host.IsDTCEnabled)
                {
                    // distributed transaction!!!!
                    using (var trans = new TransactionScope(TransactionScopeOption.RequiresNew))
                    {
                        ExecuteSql();
                        trans.Complete();
                    }
                }
                else
                {
                    ExecuteSql();
                }
                result.status = "success";
            }
            catch (Exception ex)
            {
                result.status = "error";
                if (_host.IsDebugConfiguration)
                {
                    result.message = ex.Message;
                }
                else
                {
                    result.message = "Unable to delete user";
                }
            }
            return(result);
        }