示例#1
0
        public bool Update(int id, LicenserInfoModel model)
        {
            try
            {
                using (var db = new LicenseDbEntities())
                {
                    var dbModel = db.LicenseOwners.FirstOrDefault(x => x.Id == id);
                    if (dbModel != null)
                    {
                        var result = DbHelper.CreateDbModel(model, dbModel);

                        db.SaveChanges();

                        return(true);
                    }

                    return(false);
                }
            }
            catch (Exception ex)
            {
                var error = ((System.Data.Entity.Validation.DbEntityValidationException)ex).EntityValidationErrors;
                _logger.Log(LogLevel.Error, ex);

                throw;
            }
        }
示例#2
0
        public bool AddServer(AddServerRequest request)
        {
            try
            {
                bool result = false;
                using (var db = new LicenseDbEntities())
                {
                    var owner = db.LicenseOwners.FirstOrDefault(x => x.RegNom == request.RegNom);
                    if (owner != null)
                    {
                        foreach (var x in request.Servers)
                        {
                            var server = db.LicenseOwnerServers
                                         .FirstOrDefault(v => v.ServerInstance == x.ServerInstance &&
                                                         v.LicenseOwnerID == owner.Id &&
                                                         v.SendFromPC == request.ComputerName &&
                                                         v.SystemUserName == request.SystemUserName);
                            if (server == null)
                            {
                                db.LicenseOwnerServers.Add(new LicenseOwnerServer()
                                {
                                    LicenseOwnerID      = owner.Id,
                                    ServerInstance      = x.ServerInstance,
                                    ServerIPAddress     = x.ServerIPAddress,
                                    SendFromPC          = request.ComputerName,
                                    SendFromPCIPAddress = request.ComputerIP,
                                    CreateDate          = request.RequestDate,
                                    SystemUserName      = request.SystemUserName
                                });
                            }
                            else
                            {
                                server.ServerIPAddress     = x.ServerIPAddress;
                                server.SendFromPC          = request.ComputerName;
                                server.SendFromPCIPAddress = request.ComputerIP;
                                server.CreateDate          = request.RequestDate;
                                server.SystemUserName      = request.SystemUserName;
                            }
                        }

                        db.SaveChanges();

                        return(true);
                    }
                }

                return(result);
            }
            catch (Exception ex)
            {
                var error = ((System.Data.Entity.Validation.DbEntityValidationException)ex).EntityValidationErrors;
                _logger.Log(LogLevel.Error, ex);

                throw;
            }
        }
示例#3
0
        public bool Delete(int id)
        {
            using (var db = new LicenseDbEntities())
            {
                var result = db.IpFilters.FirstOrDefault(x => x.Id == id);
                if (result != null)
                {
                    db.IpFilters.Remove(result);
                }

                db.SaveChanges();
            }

            return(true);
        }
示例#4
0
        public bool Delete(string id)
        {
            using (var db = new LicenseDbEntities())
            {
                var result = db.Licenses.FirstOrDefault(x => x.Id == new Guid(id));
                if (result != null)
                {
                    result.Enabled = false;
                    db.SaveChanges();

                    return(true);
                }
            }

            return(false);
        }
示例#5
0
 public void UpdateVariables(string licenseId, Dictionary <string, object> values)
 {
     using (var db = new LicenseDbEntities())
     {
         var licenseGuid = Guid.Parse(licenseId);
         foreach (var v in values)
         {
             var variable = db.LicenseVariables.FirstOrDefault(x => x.lu_LicenseVariables.Name == v.Key);
             if (variable != null)
             {
                 variable.Value = v.Value != null?v.Value.ToString() : string.Empty;
             }
         }
         db.SaveChanges();
     }
 }
示例#6
0
        public bool UpdateLookupVariable(VariableModel model)
        {
            using (var db = new LicenseDbEntities())
            {
                var variable = db.lu_LicenseVariables.FirstOrDefault(x => x.Id == model.Id);
                if (variable == null)
                {
                    return(false);
                }

                variable.Name = model.Name;
                variable.Type = model.Type;
                db.SaveChanges();
            }

            return(true);
        }
示例#7
0
        public void DeleteVariables(string licenseId, List <string> variables)
        {
            using (var db = new LicenseDbEntities())
            {
                var licenseGuid = Guid.Parse(licenseId);
                foreach (var v in variables)
                {
                    var variable = db.LicenseVariables.FirstOrDefault(x => x.lu_LicenseVariables.Name == v);
                    if (variable != null)
                    {
                        db.LicenseVariables.Remove(variable);
                    }
                }

                db.SaveChanges();
            }
        }
示例#8
0
        public void Log(ApiLogEntry logEntry)
        {
            var result = new ApiLog()
            {
                AbsoluteUri         = logEntry.AbsoluteUri,
                Host                = logEntry.Host,
                RequestBody         = logEntry.RequestContentBody,
                RequestIpAddress    = logEntry.RequestIpAddress,
                RequestMethod       = logEntry.RequestMethod,
                RequestTimestamp    = logEntry.RequestTimestamp.Value,
                RequestUri          = logEntry.RequestUri,
                ResponseContentBody = logEntry.ResponseContentBody,
                ResponseStatusCode  = logEntry.ResponseStatusCode,
                ResponseTimestamp   = logEntry.ResponseTimestamp
            };

            _db.ApiLogs.Add(result);
            _db.SaveChanges();
        }
示例#9
0
        private void LogLicenseChange(LicenseDbEntities db,
                                      bool isDemo,
                                      string oldObject,
                                      string newObject,
                                      Guid id,
                                      long?userId)
        {
            db.LicensesLogs.Add(new LicensesLog()
            {
                LicenseId = id,
                Date      = DateTime.Now,
                IsDemo    = isDemo,
                Old       = oldObject,
                New       = newObject,
                ChangedBy = userId ?? 0
            });

            db.SaveChanges();
        }
示例#10
0
        public bool Add(IpAddressElement ipAddressElement)
        {
            using (var db = new LicenseDbEntities())
            {
                if (db.IpFilters.Any(x => x.Address == ipAddressElement.Address))
                {
                    return(false);
                }

                db.IpFilters.Add(new IpFilter()
                {
                    Address = ipAddressElement.Address.Trim(),
                    Denied  = ipAddressElement.Denied
                });

                db.SaveChanges();
            }

            return(true);
        }
示例#11
0
        public void SetUseIpFiltering(bool useRestriction)
        {
            using (var db = new LicenseDbEntities())
            {
                var result = db.Settings.FirstOrDefault();
                if (result != null)
                {
                    result.UseIPFilter = useRestriction;
                }
                else
                {
                    db.Settings.Add(new Setting()
                    {
                        UseIPFilter = useRestriction
                    });
                }

                db.SaveChanges();
            }
        }
示例#12
0
        public bool CreateLookupVariable(string name, string type = null)
        {
            using (var db = new LicenseDbEntities())
            {
                var variable = db.lu_LicenseVariables.FirstOrDefault(x => x.Name == name);
                if (variable != null)
                {
                    return(false);
                }

                var result = new lu_LicenseVariables()
                {
                    Name = name, Type = type
                };
                db.lu_LicenseVariables.Add(result);
                db.SaveChanges();
            }

            return(true);
        }
示例#13
0
        public bool Edit(int id, IpAddressElement ipAddressElement)
        {
            using (var db = new LicenseDbEntities())
            {
                if (db.IpFilters.Any(x => x.Address == ipAddressElement.Address && x.Id != id))
                {
                    return(false);
                }

                var result = db.IpFilters.FirstOrDefault(x => x.Id == id);
                if (result != null)
                {
                    result.Address = ipAddressElement.Address.Trim();
                    result.Denied  = ipAddressElement.Denied;

                    db.SaveChanges();
                }
            }

            return(true);
        }
示例#14
0
        public bool Create(LicenserInfoModel model)
        {
            try
            {
                using (var db = new LicenseDbEntities())
                {
                    var result = DbHelper.CreateDbModel(model);

                    db.LicenseOwners.Add(result);
                    db.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                var error = ((System.Data.Entity.Validation.DbEntityValidationException)ex).EntityValidationErrors;
                _logger.Log(LogLevel.Error, ex);

                throw;
            }
        }
示例#15
0
        public void CreateVariables(string licenseId, Dictionary <string, object> values)
        {
            using (var db = new LicenseDbEntities())
            {
                var licenseGuid = Guid.Parse(licenseId);
                foreach (var v in values)
                {
                    var variable = db.lu_LicenseVariables.FirstOrDefault(x => x.Name == v.Key);
                    if (variable != null)
                    {
                        int intValue;
                        if (variable.Type == ((int)VariableTypeEnum.Integer).ToString() && v.Value != null &&
                            !int.TryParse(v.Value.ToString(), out intValue))
                        {
                            continue;
                        }

                        var licenseVariable = db.LicenseVariables.FirstOrDefault(x => x.lu_LicenseVariables.Id == variable.Id &&
                                                                                 x.LicenseId == licenseGuid);
                        if (licenseVariable != null)
                        {
                            licenseVariable.Value = v.Value != null?v.Value.ToString() : string.Empty;
                        }
                        else
                        {
                            db.LicenseVariables.Add(new LicenseVariable()
                            {
                                LicenseId  = licenseGuid,
                                VariableId = variable.Id,
                                Value      = v.Value != null ? v.Value.ToString() : string.Empty
                            });
                        }
                    }
                }

                db.SaveChanges();
            }
        }
示例#16
0
        public string Create(LicenseModel model, long?userId = null)
        {
            try
            {
                using (var db = new LicenseDbEntities())
                {
                    var owner = db.LicenseOwners.FirstOrDefault(x => x.Id == model.User.Id);
                    if (owner == null)
                    {
                        owner = model.User.IsCompany
                            ? db.LicenseOwners.FirstOrDefault(x => x.CompanyId == model.User.CompanyId)
                            : db.LicenseOwners.FirstOrDefault(x => x.EGN == model.User.EGN);
                    }

                    if (owner == null)
                    {
                        owner = new LicenseOwner()
                        {
                            Name          = model.User.Name,
                            IsCompany     = model.User.IsCompany,
                            Email         = model.User.Email,
                            Phone         = model.User.Phone,
                            ContactPerson = model.User.ContactPerson,
                            CompanyId     = model.User.CompanyId,
                            EGN           = model.User.EGN
                        };
                    }

                    var extraInfo = owner.LicenseOwnerExtraInfoes1 != null?owner.LicenseOwnerExtraInfoes1.FirstOrDefault() : null;

                    if (extraInfo == null)
                    {
                        if (model.User.PostCode > 0 ||
                            !string.IsNullOrEmpty(model.User.PostAddress) ||
                            !string.IsNullOrEmpty(model.User.PostAddress) ||
                            !string.IsNullOrEmpty(model.User.RegistrationAddress) ||
                            !string.IsNullOrEmpty(model.User.MOL) ||
                            !string.IsNullOrEmpty(model.User.AccountingPerson) ||
                            !string.IsNullOrEmpty(model.User.ContactPerson))
                        {
                            extraInfo = new LicenseOwnerExtraInfo1();
                            extraInfo.LicenseOwnerId = owner.Id;

                            var userInfo = (LicenserInfoModel)model.User;

                            extraInfo.PostCode            = userInfo.PostCode;
                            extraInfo.PostAddress         = userInfo.PostAddress;
                            extraInfo.RegistrationAddress = userInfo.RegistrationAddress;
                            extraInfo.MOL              = userInfo.MOL;
                            extraInfo.ContactPerson    = userInfo.ContactPerson;
                            extraInfo.AccountingPerson = userInfo.AccountingPerson;
                            extraInfo.VatRegistration  = userInfo.VATRegistration;

                            owner.LicenseOwnerExtraInfoes1.Add(extraInfo);
                        }
                    }

                    var result = new License()
                    {
                        Id             = Guid.NewGuid(),
                        IsDemo         = model.IsDemo,
                        ValidTo        = !model.IsDemo ? model.ValidTo : DateTime.Now.AddMonths(1),
                        SubscribedTo   = !model.IsDemo ? model.SubscribedTo : DateTime.Now.AddMonths(1),
                        Type           = !model.IsDemo ? (byte)model.Type : (byte)LicenseTypeEnum.PerComputer,
                        LicenseOwner   = owner,
                        LicenseModules = model.LicenseModules.Select(x => new LicenseModule()
                        {
                            ModuleId = (short)x,
                            ValidTo  = model.ValidTo
                        }).ToList(),
                        Enabled           = !model.IsDemo ? false : true,//the real license should be enabled, afterwards e.g. after it is payed
                        CreatedDate       = DateTime.Now,
                        WorkstationsCount = model.Type == LicenseTypeEnum.PerUser ? 1 : model.WorkstationsCount.Value
                    };

                    var created = db.Licenses.Add(result);
                    db.SaveChanges();

                    var id = created.Id;

                    if (id != Guid.Empty)
                    {
                        LogLicenseChange(db, result.IsDemo,
                                         null,
                                         Serialize(result), id, userId);
                    }

                    return(id.ToString());
                }
            }
            catch (Exception ex)
            {
                var error = ((System.Data.Entity.Validation.DbEntityValidationException)ex).EntityValidationErrors;
                _logger.Log(LogLevel.Error, ex);

                throw;
            }
        }
示例#17
0
        public bool CheckOrActivate(LicenseModel license, string activationKey, string computerName)
        {
            bool result = false;

            using (var db = new LicenseDbEntities())
            {
                var licenseDb = db.Licenses.FirstOrDefault(x => x.Id == license.Id);
                if (licenseDb == null)
                {
                    return(false);
                }

                switch (license.Type)
                {
                case LicenseTypeEnum.PerComputer:
                    bool found = false;
                    foreach (var activation in licenseDb.LicenseActivations)
                    {
                        if (activation.ComputerId == activationKey)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (found)
                    {
                        result = true;
                    }
                    else
                    {
                        if (licenseDb.LicenseActivations.Count >= licenseDb.WorkstationsCount)
                        {
                            result = false;
                        }
                        else
                        {
                            licenseDb.LicenseActivations.Add(new LicenseActivation()
                            {
                                ComputerId = activationKey, ComputerName = computerName
                            });
                            result = true;
                        }
                    }
                    break;

                case LicenseTypeEnum.PerUser:
                    if (licenseDb.LicenseActivations.Count == 0)
                    {
                        licenseDb.LicenseActivations.Add(new LicenseActivation()
                        {
                            UserId = activationKey, ComputerName = computerName
                        });
                    }

                    result = licenseDb.LicenseActivations.Any(x => x.UserId == activationKey);
                    break;

                case LicenseTypeEnum.PerServer:
                    result = true;
                    break;
                }

                db.SaveChanges();
            }

            return(result);
        }
示例#18
0
        public bool Update(string id, UpdateLicenseModel model, long?userId = null)
        {
            using (var db = new LicenseDbEntities())
            {
                var result = db.Licenses.FirstOrDefault(x => x.Id == new Guid(id));
                if (result != null)
                {
                    string beforeUpdate = Serialize(result);

                    result.ValidTo           = model.ValidTo ?? result.ValidTo;
                    result.SubscribedTo      = model.SubscribedTo ?? result.SubscribedTo;
                    result.IsDemo            = model.IsDemo ?? result.IsDemo;
                    result.Enabled           = model.Enabled ?? result.Enabled;
                    result.Type              = (byte?)model.Type ?? result.Type;
                    result.WorkstationsCount = model.Type == LicenseTypeEnum.PerUser ? 1 : model.ComputerCount;

                    if (model.UserId != result.LicenseOwner.Id)
                    {
                        result.LicenseOwner = db.LicenseOwners.First(x => x.Id == model.UserId);
                    }

                    #region Modules
                    if (model.Modules != null)
                    {
                        var modulesIds = model.Modules.Select(x => (short)x)
                                         .ToList();

                        foreach (var moduleId in modulesIds)
                        {
                            if (result.LicenseModules.FirstOrDefault(x => x.ModuleId == moduleId) == null)
                            {
                                result.LicenseModules.Add(new LicenseModule()
                                {
                                    ModuleId = moduleId,
                                    ValidTo  = result.ValidTo
                                });
                            }
                        }

                        var modulesForRemoval = new List <LicenseModule>();
                        foreach (var module in result.LicenseModules)
                        {
                            if (!modulesIds.Contains(module.ModuleId))
                            {
                                modulesForRemoval.Add(module);
                            }
                        }

                        foreach (var module in modulesForRemoval)
                        {
                            db.LicenseModules.Remove(module);
                        }
                    }
                    #endregion

                    db.SaveChanges();

                    LogLicenseChange(db, result.IsDemo,
                                     beforeUpdate,
                                     Serialize(result), result.Id, userId);

                    return(true);
                }
            }

            return(false);
        }
示例#19
0
        public bool Activate(Guid id, LicenseActivateModel model)
        {
            bool result = false;

            using (var db = new LicenseDbEntities())
            {
                var licenseDb = db.Licenses.FirstOrDefault(x => x.Id == id);
                if (licenseDb == null)
                {
                    return(false);
                }
                var server = db.LicenseOwnerServers.FirstOrDefault(x => x.LicenseOwnerID == licenseDb.LicenseOwnerId &&
                                                                   x.ServerInstance == model.ServerName);
                if (server == null)
                {
                    server = new LicenseOwnerServer()
                    {
                        LicenseOwnerID = licenseDb.LicenseOwnerId,
                        ServerInstance = model.ServerName,
                        CreateDate     = DateTime.Now
                    };
                    db.LicenseOwnerServers.Add(server);
                }

                switch ((LicenseTypeEnum)licenseDb.Type)
                {
                case LicenseTypeEnum.PerComputer:
                    if (licenseDb.LicenseActivations.Count < licenseDb.WorkstationsCount)
                    {
                        licenseDb.LicenseActivations.Add(new LicenseActivation()
                        {
                            ComputerId           = model.ActivationKey,
                            ComputerName         = model.ComputerName,
                            LicenseOwnerServerId = server.LicenseOwnerID
                        });
                        result = true;
                    }
                    break;

                case LicenseTypeEnum.PerUser:
                    if (licenseDb.LicenseActivations.Count == 0)
                    {
                        licenseDb.LicenseActivations.Add(new LicenseActivation()
                        {
                            UserId               = model.ActivationKey,
                            ComputerName         = model.ComputerName,
                            LicenseOwnerServerId = server.LicenseOwnerID
                        });
                    }
                    break;

                case LicenseTypeEnum.PerServer:
                    result = true;
                    break;
                }

                db.SaveChanges();
            }

            return(result);
        }