Exemplo n.º 1
0
        public CrudeDefaultUserContract FetchByDefaultUserName(string defaultUserName)
        {
            var dataAccessLayer = new CrudeDefaultUserData();
            var contract        = new CrudeDefaultUserContract();

            dataAccessLayer.FetchByDefaultUserName(defaultUserName);
            DataToContract(dataAccessLayer, contract);

            return(contract);
        }
        /// <summary>
        /// Login a user and log the activity
        /// </summary>
        /// <param name="userCode">IP address will be resolved to whois netname, valid usercode stays</param>
        /// <returns>Internal user id</returns>
        public Guid Login(
            string userCode,
            bool overrideUserName
            )
        {
            CrudeDefaultUserData defaultUser = new CrudeDefaultUserData();

            try {
                defaultUser.FetchByDefaultUserName(userCode);

                // insert if not existing
                if (defaultUser.DefaultUserId == Guid.Empty)
                {
                    defaultUser.DefaultUserId   = new Guid();
                    defaultUser.DefaultUserCode = userCode;

                    // resolve net-name if user code appears to be an ip address
                    IPAddress ipAddress;
                    if (IPAddress.TryParse(userCode, out ipAddress))
                    {
                        defaultUser.DefaultUserName = ResolveNetnameFromIpV4(userCode);

                        // use ip user code if no net-name found
                        if (string.IsNullOrEmpty(defaultUser.DefaultUserName))
                        {
                            defaultUser.DefaultUserName = userCode;
                        }
                    }
                    else
                    {
                        // not an ip address, username is usercode
                        defaultUser.DefaultUserName = userCode;
                    }

                    defaultUser.DefaultStateRcd        = DefaultStateRef.Created;
                    defaultUser.DateTime               = DateTime.UtcNow;
                    defaultUser.CreatedByDefaultUserId = new Guid("FFFFFFFF-5555-5555-5555-FFFFFFFFFFFF");
                    defaultUser.Insert();

                    AddActivity(
                        defaultUser.DefaultUserId,
                        DefaultUserActivityTypeRef.Login,
                        "New user: "******"Internal"
                        );
                }
                else
                {
                    // resolve net-name if user name appears to be an ip address
                    // or an averride is asked for
                    IPAddress ipAddress;
                    if (IPAddress.TryParse(defaultUser.DefaultUserName, out ipAddress) ||
                        overrideUserName
                        )
                    {
                        string netName = ResolveNetnameFromIpV4(userCode);

                        // use new net-name if found
                        if (!string.IsNullOrEmpty(netName))
                        {
                            // a new net name was found, use that instead of ip address
                            defaultUser.DefaultUserName = netName;
                            defaultUser.DefaultStateRcd = DefaultStateRef.Updated;
                            defaultUser.DateTime        = DateTime.UtcNow;
                            defaultUser.Update();

                            AddActivity(
                                defaultUser.DefaultUserId,
                                DefaultUserActivityTypeRef.Login,
                                "Resolved user name: "
                                + defaultUser.DefaultUserName
                                + " from IP address: "
                                + userCode,
                                "Internal"
                                );
                        }
                    }
                }

                AddActivity(
                    defaultUser.DefaultUserId,
                    DefaultUserActivityTypeRef.Login,
                    defaultUser.DefaultUserName,
                    "Internal"
                    );
            } catch (Exception ex) {
                // todo, what happens with WCF here?
                throw new Exception("Login failed", ex);
            }

            return(defaultUser.DefaultUserId);
        }