FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour WNetGetUser() y nombres de usuario
Posts: 4
Joined: Mon Mar 10, 2014 11:34 AM
WNetGetUser() y nombres de usuario
Posted: Mon Mar 10, 2014 05:06 PM

Muy buenas a todos y gracias por vuestra ayuda.

Mi cuesti贸n se plantea con WNetGetUser(), que como bien sabr茅is capta el nombre del usuario que, por ejemplo ejecuta una aplicaci贸n y proceso, pero que me plantea un peque帽o problema. El nombre de usuario que recoge es el que en el momento de la instalaci贸n del equipo se denomin贸, y por mucho que luego cambiemos el nombre de usuario desde panel de control, WNetGetUser() nos seguir谩 devolviendo el mismo.

Este problema es muy com煤n para aquellas empresas que montan equipos desde una imagen previa: Todos los ordenadores tendr谩n el mismo nombre. Y tambi茅n para aquellos que cambian equipos entre diferente personal.

No he probado crear una nueva cuenta de usuario dentro del propio Windows, y se que la manera de resolverlo es trasteando el registro y cambiando los nombres de los directorios donde se guarda la informaci贸n de cada usuario.

驴Pod茅is indicarme otra soluci贸n m谩s sencilla?

Gracias por vuestra ayuda

Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: WNetGetUser() y nombres de usuario
Posted: Mon Mar 10, 2014 06:55 PM
Gabriel
-- google translate
Very good to all and thanks for your help.

My question arises WNetGetUser (), which as you will know captures the user name, for example and run a process, but I posed a little problem. The username is the collecting was called at the time of installation of the equipment, and then much to change the username from the control panel, WNetGetUser () will return the same to us.

This problem is very common for companies that assemble computers from a previous image: All computers have the same name. And for those that change between different staff teams.

I have not tried creating a new user account within Windows itself, and that the way to solve it is messing the registry and changing the names of the directories where the information is stored for each user.

Can you tell me a simpler solution?

Thanks for your help


Any business that is running a Microsoft AD network automatically generates the new ( client ) user that has just logged in if the account does not exist on the machine.

If the business you describe does not have a Microsoft AD network .. then ( as you mention ) the cloned 'user' will be the same for each machine.

When I use WNetGetUser() .. I bounce the results against a User Table maintained in my application... if that result does not match I throw up a login screen for each user to put in their own userid that I have assigned in the User Table. That way you control the users ( id ) in your table and can then assign roles to those users to manager rights in your application.

Consider ONLY the "logic" in this code ..the data tables are ADO and not DBF ..

Rick Lipkin
Code (fw): Select all Collapse
// check for valid user and get and set global rights
// login.prg

lOK 聽 聽:= .F.
oDLG 聽 := NIL
cSAY 聽 := " "

xREAD 聽:= " "
xWRITE := " "
xLOGIN := " "
xSUPER := " "
xAdmin := " "

cAUTH 聽:= 'N'
lOK1 聽 := .F.
DEFINE DIALOG oDlg 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽;
聽 聽 聽 聽FROM 5, 8 to 10, 75 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽;
聽 聽 聽 聽TITLE "Logging In ...... Please be patient" 聽 聽 聽 聽 聽 聽;
聽 聽 聽 聽STYLE nOr( WS_POPUP,WS_CAPTION,WS_THICKFRAME )

聽 聽 聽 聽cSAY := 聽"Validating User "

聽 聽 聽 聽@ 1,13 SAY oSAY VAR cSAY of oDLG //COLOR "N/W"

聽 聽 聽 聽oDLG:bStart := { || lOK1 := _Login( cAUTH, oDLG, @oSAY, @cSAY)}

ACTIVATE DIALOG oDLG CENTERED

IF lOK1 = .F.
聽 聽RETURN(NIL)
ENDIF

IF xLOGIN = "UNKNOWN"

聽 聽SAYING := "The System could not resolve your UserID "+CHR(10)
聽 聽SAYING += UPPER( WNetGetuser() )+CHR(10)
聽 聽SAYING += "Would you like to Login Manually ?"+CHR(10)

聽 聽IF MsgYesNo( SAYING )
聽 聽 聽 IF _log_in("L") 聽 聽 聽 聽 聽 聽 聽// log_in.prg
聽 聽 聽 ELSE
聽 聽 聽 聽 聽CLOSE DATABASES
聽 聽 聽 聽 聽RETURN(NIL)
聽 聽 聽 ENDIF
聽 聽ELSE
聽 聽 聽 CLOSE DATABASES
聽 聽 聽 RETURN(NIL)
聽 聽ENDIF

ENDIF

...
...

// end



//---------------------
FUNC _LOGIN( cAUTH, oDLG, oSAY, cSAY )

LOCAL cNAME,SAYING,nPOS,cDEFA,lOK := .F.
LOCAL oRsStaff,cSql,oErr,cFind,lFirst
Local nEid

#INCLUDE "FIVEWIN.CH"

// -- initial setup

cDEFA 聽:= SET(7)
xLOGIN := substr(upper(WNetGetuser()+space(25)),1,25) 聽 // fivewin

cSAY := "Validating User "+xLOGIN
oSAY:ReFresh()
SysReFresh()

lFirst := .f.

oRsStaff := TOleAuto():New( "ADODB.Recordset" )
oRsStaff:CursorType 聽 聽 := 1 聽 聽 聽 聽// opendkeyset
oRsStaff:CursorLocation := 3 聽 聽 聽 聽// local cache
oRsStaff:LockType 聽 聽 聽 := 3 聽 聽 聽 聽// lockoportunistic

// redundant check .. should never happen unless
// all staff users get deleted

cSql := "Select * From [Staff] where [UserId] = '"+alltrim(xLOGIN)+"'"

TRY
聽 聽oRsStaff:Open( cSQL, xCONNECT )
CATCH oErr
聽 聽MsgInfo( "Error in Opening STAFF table" )
聽 聽oDLG:END()
聽 聽RETURN(.F.)
END TRY

IF oRsStaff:eof
聽 聽xLOGIN := "UNKNOWN"
聽 聽xREAD 聽:= 'Y'
聽 聽xWRITE := 'N'
聽 聽xSUPER := 'N'
ELSE

聽 聽xREAD 聽 := if(empty(oRsStaff:Fields("ReadOnly"):Value), "Y",oRsStaff:Fields("ReadOnly"):Value )
聽 聽xWRITE 聽:= if(empty(oRsStaff:Fields("WriteOnly"):Value),"N",oRsStaff:Fields("WriteOnly"):Value )
聽 聽xSUPER 聽:= if(empty(oRsStaff:Fields("Super"):Value), 聽 聽"N",oRsStaff:Fields("Super"):Value )
聽 聽xLogin 聽:= alltrim(oRsStaff:Fields("UserId"):Value) 聽// 25

聽 聽oRsStaff:Fields("LastLog"):Value := dtoc(date())+" "+time()
聽 聽oRsStaff:Update()

ENDIF

oRsStaff:Close()
oRsStaff := nil
oDLG:END()

RETURN(.T.)

//---------------------
FUNC _log_in( cFROM )

Local lOK
Local oBTN1,oBTN2,oUSERID,oPASS,cUSERID,cPASS,SAYING
Local oDLG, oRsUser, oErr, cSQL, oBMP

Local cMODE := "E"
Local cDEFA := SET(7)

// cFROM is where the variable was passed
// 聽if "M" .. main menu login .. if password is blank 聽deny login
// 聽if "L" .. first login screen .. allow

IF EMPTY( cFROM )
聽 聽cFROM := "M"
聽* 聽 cFROM := "L"
ENDIF

lOK := .F.

oRsUser := TOleAuto():New( "ADODB.Recordset" )
oRsUser:CursorType 聽 聽 := 1 聽 聽 聽 聽// opendkeyset
oRsUser:CursorLocation := 3 聽 聽 聽 聽// local cache
oRsUser:LockType 聽 聽 聽 := 3 聽 聽 聽 聽// lockoportunistic

cSQL := "SELECT * From [Staff] Order by [Lname]"

TRY
聽 oRsUser:Open(cSQL,xCONNECT )
CATCH oErr
聽 MsgInfo( "Error in Opening Staff table" )
聽 RETURN(.F.)
END TRY

cUSERID 聽 := SPACE(25)
cPASS 聽 聽 := SPACE(15)

//------

DEFINE BITMAP oBmp RESOURCE "USERVIEW"
DEFINE DIALOG oDlg RESOURCE "LOGIN"

聽 聽REDEFINE GET oUSERID var cUSERID ID 110 of oDLG 聽 UPDATE
聽 聽REDEFINE GET oPASS 聽 var cPASS 聽 ID 120 of oDLG 聽 UPDATE

聽 聽REDEFINE BTNBMP oBtn1 ID 111 OF oDlg 聽 ;
聽 聽 聽 聽 聽RESOURCE "OK", "DOK", "DOK" ;
聽 聽 聽 聽 聽PROMPT " 聽&Ok 聽 聽" LEFT 2007;
聽 聽 聽 聽 聽ACTION ( IF(cMODE = "V",lOK := .T. , lOK := _busrules( oRsUser, cFROM,;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 @cUserid,@cPass,oUserId,oPass,@lOk,oDlg) ), ;
聽 聽 聽 聽 聽 聽 聽 聽 聽 IF(cMODE = "V",lOK := .T. , if(lOK = .T., _doit( oRsUser,cUserId,cPass,@lOk,oDlg), )),;
聽 聽 聽 聽 聽 聽 聽 聽 聽 IF(lOK = .T., oDLG:END(), ))

聽 聽REDEFINE BTNBMP oBtn2 ID 112 OF oDlg 聽 ;
聽 聽 聽 聽 聽RESOURCE "CANCEL", "DCANCEL", "DCANCEL" ;
聽 聽 聽 聽 聽PROMPT "&Cancel 聽 " LEFT 2007;
聽 聽 聽 聽 聽ACTION ( lOK := .F., oDLG:END() )


ACTIVATE DIALOG oDlg CENTERED ;
聽 聽 聽 聽 聽ON PAINT (PalBmpDraw( hDC, 0, 0, oBmp:hBitmap ))


oBmp:End()
oRsUser:Close()

RETURN(lOK)

//----------------------------
Static FUNC _Busrules( oRsUser,cFROM,cUserid,cPass,oUserId,oPass,lOk,oDlg )

LOCAL SAYING,xPASS,xUserid

IF EMPTY( cUSERID ) 聽.or. cUSERID = " 聽"
聽 聽SAYING := "SORRY ... USERID can not be left Blank"
聽 聽MsgInfo( SAYING )
聽 聽oUSERID:SetFocus()
聽 聽RETURN(.F.)
ENDIF

IF EMPTY( cPASS ) .or. cPASS = " 聽"
聽 聽SAYING := "SORRY ... PASSWORD can not be left Blank"
聽 聽MsgInfo( SAYING )
聽 聽oPASS:SetFocus()
聽 聽RETURN(.F.)
ENDIF

cUserId := upper(cUSERID)
cPASS 聽 := substr(alltrim( cPASS )+space(15),1,15)

oRsUser:MoveFirst()
oRsUser:Find("[UserId] = '"+cUSERID+"'" )

IF oRsUser:eof
聽 聽SAYING := "SORRY ... UserId 聽 "+alltrim(cUSERID)+" 聽 could not be found "+CHR(10)
聽 聽SAYING += "Would you like to try again ?"+CHR(10)

聽 聽IF MsgYesNo( SAYING )
聽 聽 聽 cUSERID := SPACE(25)
聽 聽 聽 oUSERID:ReFresh()
聽 聽 聽 oUSERID:SetFocus()
聽 聽 聽 RETURN(.F.)
聽 聽ELSE
聽 聽 聽 lOK := .F.
聽 聽 聽 RETURN(.F.)
聽 聽ENDIF
ENDIF

IF empty(oRsUser:Fields("PASSWORD"):Value) .or.;
聽 聽oRsUser:Fields("PASSWORD"):Value = " 聽"

聽 聽IF cFROM = "L"
聽 聽ELSE
聽 聽 聽 SAYING := "SORRY ... there is no Password set for "+cUSERID+CHR(10)
聽 聽 聽 SAYING += "You can not Login from the Main Menu Login screen"+chr(10)
聽 聽 聽 SAYING += "without a Password .. Please contact your SYSTEM Admin"+chr(10)
聽 聽 聽 SAYING += "to have them re-set or create your password"+chr(10)
聽 聽 聽 MsgInfo( saying )
聽 聽 聽 cPASS := SPACE(25)
聽 聽 聽 oPASS:ReFresh()
聽 聽 聽 oPASS:SetFocus()
聽 聽 聽 RETURN(.F.)
聽 聽ENDIF

聽 聽SAYING := "No Password has been set for User 聽"+cUSERID+CHR(10)
聽 聽SAYING += "Would you like '"+cPASS+"' to be your Password ?"+CHR(10)
聽 聽IF MsgYesNo( SAYING )
聽 聽 聽 oRsUser:Fields("PASSWORD"):Value := substr(ENCRYPT( cPASS )+space(15),1,15)
聽 聽 聽 oRsUser:Update()
聽 聽ELSE
聽 聽 聽 cPASS := SPACE(15)
聽 聽 聽 oPASS:ReFresh()
聽 聽 聽 oPASS:SetFocus()
聽 聽 聽 RETURN(.F.)
聽 聽ENDIF

ENDIF

xPASS := ALLTRIM(oRsUser:Fields("PASSWORD"):Value)
xPASS := DENCRYPT( xPASS )
xPASS := substr(alltrim( xPASS )+space(15),1,15)

IF cPASS = xPASS
ELSE
聽 聽SAYING := "SORRY ... You have typed in the Wrong Password "+CHR(10)
聽 聽SAYING += "Would you like to try again ?"+CHR(10)

聽 聽IF MsgYesNo( SAYING )
聽 聽 聽 cPASS:= SPACE(15)
聽 聽 聽 oPASS:ReFresh()
聽 聽 聽 oPASS:SetFocus()
聽 聽 聽 RETURN(.F.)
聽 聽ELSE
聽 聽 聽 lOK := .F.
聽 聽 聽 oDLG:END()
聽 聽 聽 Return(.f.)
聽 聽ENDIF
ENDIF

If empty(oRsUser:FIelds("Active"):Value) .or. ;
聽 聽 聽 聽 聽oRsUser:Fields("Active"):Value = "N"
聽 聽Saying := "Sorry .. "+cUSERID+" has been marked INACTIVE and can not Login"
聽 聽MsgInfo( Saying )
聽 聽lOK := .F.
聽 聽oDLG:END()
聽 聽Return(.f.)
Endif

RETURN(.T.)

//----------------------------
Static FUNC _doit( oRsUser,cUserId,cPass,lOk,oDlg )

Local cName

xLOGIN 聽 := alltrim(oRsUser:Fields("UserId"):Value)
xREAD 聽 聽:= oRsUser:Fields("READONLY"):Value
xWRITE 聽 := oRsUser:Fields("WRITEONLY"):Value
xSUPER 聽 := oRsUser:Fields("SUPER"):Value
xAdmin 聽 := " "

oRsUser:Fields("lastlog"):Value := dtoc(DATE())+" "+Time()
oRsUser:Update()


lOK := .T.
oDLG:END()

RETURN(lOK)
Posts: 4
Joined: Mon Mar 10, 2014 11:34 AM
Re: WNetGetUser() y nombres de usuario
Posted: Mon Mar 10, 2014 07:11 PM

Gracias Rick Lipkin por tu pronta respuesta, pero no es exactamente el que busco. Ya cuento con un control interno de usuarios, pero lo que me interesa saber es que ordenador se conecta. Es a efectos de proteccion de datos. La rutina que utilizo y que no acaba de funcionar por el tema que comento es la siguiente:

FUNCTION RegSusuario(cProg)

LOCAL cUsuHD:=WNetGetUser()+" "+cProg+" EL "+DTOC(DATE())+" A LAS "+TIME()+CHR(13)+CHR(10)
LOCAL cRegUsu:=oPhatBasic+"\Registro "+STR(YEAR(DATE()),4)+STR(MONTH(DATE()),2)+".TXT"
LOCAL nFichero

LOCAL cUsuHD2:=cProg+" EL "+DTOC(DATE())+" A LAS "+TIME()
LOCAL cRegUsu2:=oPhatBasic+"\Registro de "+WNetGetUser()+" "+STR(YEAR(DATE()),4)+STR(MONTH(DATE()),2)+".TXT"
LOCAL nFichero2

IF SUBSTR(cProg,1,4)="SALE"
    cUsuHD2+=" OK"
ENDIF

cUsuHD2+=CHR(13)+CHR(10)

IF FILE (cRegUsu)
    nFichero:=FOpen(cRegUsu,2)
ELSE
    nFichero:=FCreate(cRegUsu,0)
ENDIF

FSeek(nFichero,0,2)

FWrite(nFichero,cUsuHD)
FClose(nFichero)

IF FILE (cRegUsu2)
    nFichero2:=FOpen(cRegUsu2,2)
ELSE
    nFichero2:=FCreate(cRegUsu2,0)
ENDIF

FSeek(nFichero2,0,2)

FWrite(nFichero2,cUsuHD2)
FClose(nFichero2)

RETURN (.T.)

con ella creo un txt en un directorio al que solo tiene acceso el administrador para poder observar los movimientos de los equipos, as铆 como el tiempo y estado de cada uno de ellos.

Mil gracia de nuevo
Un abrazo

Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: WNetGetUser() y nombres de usuario
Posted: Mon Mar 10, 2014 07:21 PM
Gabriel


your computer is connected.


Connected as being able to see a network share ? or connected as already having the application open and logged in ?

Rick Lipkin
Posts: 4
Joined: Mon Mar 10, 2014 11:34 AM
Re: WNetGetUser() y nombres de usuario
Posted: Mon Mar 10, 2014 07:53 PM

No entiendo muy bien tu pregunta. Necesito saber que ordenador se conecta para detectar posibles intrusos.

Posts: 211
Joined: Wed Jul 16, 2008 12:59 PM
Re: WNetGetUser() y nombres de usuario
Posted: Mon Mar 10, 2014 08:05 PM
Gabriel:

Creo que deberias usar NetName() en vez de WNetGetUser(), esta funcion te da el nombre del equipo donde se esta corriendo la aplicacion.

El nombre del equipo es unico en cada LAN, no puede haber duplicados, si los hay fallaria la conexion. Es un tema de REDES.

Cada equipo en una LAN tiene un nombre unico; y si han sido instalados desde una imagen el administrador se encarga de colocarle/cambiarle a un nombre unico que lo identificara en la LAN.

Atentamente,

Rolando
Cochabamba, Bolivia.
Posts: 2170
Joined: Fri Jul 18, 2008 01:24 AM
Re: WNetGetUser() y nombres de usuario
Posted: Mon Mar 10, 2014 10:37 PM

Mira si puedes sacar algo de este post:

http://forums.fivetechsupport.com/viewt ... 21#p141189
Saludos.

Francisco J. Alegr铆a P.

Chinandega, Nicaragua.



Fwxh-MySql-TMySql
Posts: 4
Joined: Mon Mar 10, 2014 11:34 AM
Re: WNetGetUser() y nombres de usuario
Posted: Tue Mar 11, 2014 08:50 AM

Gracias RSalazarU por tu ayuda, realmente con NetName() se consigue el nombre del usuario o lo que para el windows es el USERDOMAIN.
Gracias tambi茅n a ti Silvio.Falconi por el interesante enlace.

Con todo y con eso, estoy estudiando una soluci贸n para poder cambiar el USERNAME de windows. Tan pronto la tenga probada os la detallo.

Gracias a todos por vuestra colaboraci贸n

Gabriel Martin
Barcelona, Espa帽a

Continue the discussion