예제 #1
0
        private int login(string username, string password, string pDivID, int pForceLogin, out string pDT)
        {
            int lResponse = -1;

            pDT = "";
            string lUsername, lpassword;

            _DesktopApp = true;

            lUsername = username;
            lpassword = password;

            // Preveri ali se prijavlja servis, ali uporabnik
            if ((lUsername.Length == 0) && (pDivID.Length > 0))
            {
                _DesktopApp = false;
                int lUserID = Uporabniki.Get_UserID(pDivID);
                if (lUserID > 0)
                {
                    Uporabniki.Get_UserLoginData(lUserID, out lUsername, out lpassword);
                }
            }

            string lResp = lSql.ConnectSQL(Splosno.AppSQLName);

            if (lResp.Length > 0)
            {
                throw new Exception(lResp);
            }

            StringBuilder sSQL = new StringBuilder();

            if (String.IsNullOrEmpty(lpassword))
            {
                lpassword = "";
            }
            sSQL.AppendLine(" ");
            sSQL.AppendLine("SELECT * FROM _mrt_Users ");
            sSQL.AppendLine("WHERE  acUserName = @username and acPassword = @password ");
            sSQL.AppendLine(" ");
            sSQL.AppendLine(" ");

            SqlParameter[] sqlParams = new SqlParameter[] {
                new SqlParameter("@username", lUsername),
                new SqlParameter("@password", lpassword)
            };

            string    lErr = "";
            DataTable lDT  = lSql.FillDT(sSQL, sqlParams, out lErr);

            lSql.DisconnectSQL();

            if (lDT.Rows.Count > 0)
            {
                DataRow lrow           = lDT.Rows[0];
                int     lUserID        = Convert.ToInt32(lrow["anUserID"]);
                string  lUserSessionID = (lrow["acSesionID"].ToString());
                bool    lUserActive    = Convert.ToBoolean(lrow["anActive"]);
                bool    lAppLogedIn    = Convert.ToBoolean(lrow["anAppLogedIn"]);
                bool    lLogedIn       = Convert.ToBoolean(lrow["anLogedIn"]);

                pDT = Splosno.SerializeDataTable_json(lDT);

                if (lUserActive == true)
                {
                    int maxLic;
                    //int.TryParse(Nastavitve.Get("", "LicNumber", "0"), out maxLic);
                    int.TryParse(Nastavitve.LicData["LicNumberValue"], out maxLic);
                    int  currLogedUsers  = Uporabniki.Get_LoggedUsers();
                    bool lCheckLicNumber = Convert.ToBoolean(Nastavitve.LicData["LicNumberCheck"]);

                    if ((!lCheckLicNumber) || (maxLic >= (currLogedUsers + 1)))
                    {
                        if ((lAppLogedIn != true && !lLogedIn) || (pForceLogin == 1))
                        {
                            string lNewSessionID = Guid.NewGuid().ToString();

                            // Če je uporabnik prijavljen v drugem sistemu, ohrani sesionID, da ga ne vržemo ven!
                            //if ((lLogedIn) || (lAppLogedIn)) { lNewSessionID = lUserSessionID; }

                            Set_UserUpdate(lUserID, true, lNewSessionID, _DesktopApp);

                            // Ponovno pridobi podatke o userju z novim sessionid-jem
                            DataTable lDT1 = Uporabniki.Get_UserBySession(lUsername, lNewSessionID);
                            pDT = Splosno.SerializeDataTable_json(lDT1);

                            lSql.ConnectSQL(Splosno.AppSQLName);
                            sSQL.Remove(0, sSQL.Length);
                            sSQL.AppendLine("SELECT UR.acRoleID FROM _mrt_UserRoles UR WITH(NOLOCK) ");
                            sSQL.AppendLine("WHERE (UR.acUserName = @UserName) ");

                            sqlParams = null;
                            sqlParams = new SqlParameter[] {
                                new SqlParameter("@UserName", lUsername)
                            };

                            lDT = null;
                            lDT = lSql.FillDT(sSQL, sqlParams, out lErr);
                            lSql.DisconnectSQL();

                            if (lDT.Rows.Count > 0)
                            {
                                _pravice = Splosno.SerializeDataTable_json(lDT);
                            }
                            else
                            {
                                _pravice = "[]";
                            }

                            lrow          = lDT1.Rows[0];
                            _id           = Convert.ToInt32(lrow["anUserID"]);
                            _username     = Convert.ToString(lrow["acUserName"]);
                            _ime          = Convert.ToString(lrow["acFirstName"]);
                            _priimek      = Convert.ToString(lrow["acLastName"]);
                            _ModifiedDate = Convert.ToDateTime(lrow["adTimeMod"]);
                            _active       = Convert.ToBoolean(lrow["anActive"]);
                            _AppLogedIn   = Convert.ToBoolean(lrow["anAppLogedIn"]);
                            _logged_in    = Convert.ToBoolean(lrow["anLogedIn"]);
                            _SessionID    = Convert.ToString(lrow["acSesionID"]);
                            _Email        = Convert.ToString(lrow["acEmail"]);
                            _Gsm          = Convert.ToString(lrow["acGSM"]);
                            _admin        = Convert.ToBoolean(lrow["anAdmin"]);
                            lResponse     = 0;
                        }
                        else
                        {
                            lResponse = 103; //Uporabnik je že prijavljen!
                        }
                    }
                    else
                    {
                        lResponse = 102;
                    }
                }
                else
                {
                    lResponse = 101;
                }
            }
            else
            {
                lResponse = 100;
            }

            return(lResponse);
        }