Method for realizing free switching of system user accounts based on localStorage
Technical Field
The invention relates to a technology for switching user accounts in a website or a system, in particular to a method for realizing free switching of system users based on localStorage.
Background
Since a system super administrator a is often needed to help a general user B to handle background operation requirements in the process of making a website or a system, we can make an assumption: if the system super administrator a directly intervenes in the operation, the log recorded by the system is the operation of the system super administrator a. When a common user B logs in the system later to check the operation logs, the corresponding logs cannot be checked, but the operation is already performed, so that the security of the system is suspected, and meanwhile, accurate user operation data cannot be obtained when the system or website operation logs are counted later.
In order to solve the problems of loss and confusion of the operation records of the system or the website, an account switching function is used. Therefore, how to quickly and effectively switch accounts to achieve the purpose of system super administrators intervening or helping ordinary users to operate the system without disturbing the integrity of system data is a problem which needs to be actually faced in design.
Disclosure of Invention
The purpose of the invention is as follows: the invention aims to provide a method for realizing free switching of system user accounts based on localStorage, so that a system super administrator A can not disturb the integrity of system data when switching roles, authorities and the like of a common user B.
The technical scheme is as follows: in order to achieve the technical effects, the invention provides the following technical scheme:
a method for realizing free switching of system user accounts based on localStorage is characterized by comprising the following steps:
(1) the method comprises the steps that a system foreground establishes a localStorage file for each user needing to store user information respectively, and stores corresponding user information in the localStorage file;
(2) when the super administrator A intervenes or helps the common user B to operate the system, the super administrator A calls an interface of the system background switching common user B;
(3) when receiving a user switching request, a system background judges whether a requester is a system super manager A, if so, the background switches the session or cookie content of the super manager A recording user information into the user information of a common user B, and then returns the user information of the common user B to the super manager A; if not, the system background returns a prompt of incapability of switching to the super administrator A;
(4) after receiving the user information of the common user B returned by the background, the system foreground backs up the user information in the localStorage file C of the super administrator A to a new localStorage file C*Performing the following steps; then, replacing the user information in the localStorage file C of the super administrator A with the user information of the common user B returned by the background;
(5) when the operation of the system super manager A is finished and the identity is switched back, the backup is acquired firstlylocalStorage file C*Judgment of C*Whether there is a value; if the current user is switched by the system super manager A, the system super manager A is switched back at the moment, the session or cookie content, which is stored by the common user B at the moment and records the user information, of the background is switched into the user information of the super manager A by calling a background interface, the information of the super manager A is returned and stored in a localStorage file C, the information of the common user B is stored before being replaced, and the localStorage file C is emptied from the foreground*(ii) a If the value is not the same, judging that the current user is not switched by the system super administrator A or the login information is invalid, and prompting to log in again.
Furthermore, before the super administrator a intervenes or helps the operating system of the common user B, the super administrator a searches the identity information of the common user B, queries the database through the background code to find the common user B, and obtains the identity label of the common user B.
Further, the system searches for the common user B through an identity which can uniquely confirm the identity of the user, wherein the identity comprises a user name, an ID, a role and authority.
Has the advantages that: compared with the prior art, the invention has the following advantages:
based on the storage characteristic of the localStorage, the user information of the user login is stored by adopting the localStorage file, when the super administrator A intervenes or helps the common user B to operate the system, the user information in the localStorage file of the super administrator A of the system is replaced by the user information of the common user B, which is equivalent to that the common user B performs system operation, all operations related to the user information, such as background log recording, and the like, cannot be changed, the information of the common user B is recorded, and the problems of loss and disorder of system or website operation records can be perfectly solved.
Drawings
FIG. 1 is a flow chart of the present invention.
Detailed Description
The technical scheme of the invention is further explained by combining the attached drawings and the detailed embodiment.
Example (b): fig. 1 is a flowchart of the present embodiment, which includes the following steps:
step one, a system super manager A can uniquely confirm the condition of the common user B by searching the input user name, ID and the like to inquire the corresponding common user B.
And step two, the system super administrator A calls an interface of the background switching common user B.
And step three, the background receives a user switching request, judges whether the role of the user requesting the user is a system super manager A, if so, switches the session or cookie of the background system super manager A for recording the user information into the user information of a common user B, and returns the user information of the common user B, and if not, returns a prompt of incapability of switching.
Step four, backing up the user information in the localStorage file C of the foreground existing system super manager A to a new localStorage file C*In (1).
And step five, replacing the user information in the localStorage file C of the foreground existing system super administrator A with the information of the common user B returned by the background.
Step six, when the operation of the system super administrator A is finished and the system super administrator A wants to switch back to the identity, firstly, a backup localStorage file C is obtained*Judgment of C*Whether there is a value; if the current user is switched by the system super manager A, the system super manager A is switched back at the moment, the session or cookie content, which is stored by the common user B at the moment and records the user information, of the background is switched into the user information of the super manager A by calling a background interface, the information of the super manager A is returned and stored in a localStorage file C, the information of the common user B is stored before being replaced, and the localStorage file C is emptied from the foreground*(ii) a If the value is not the same, judging that the current user is not switched by the system super administrator A or the login information is invalid, and prompting to log in again.
In the process of the above operation steps, since the localStorage of the system super administrator a has been switched to the information of the ordinary user B, it is equivalent to that the ordinary user B performs system operation, all operations related to user information, such as log recording in the background, etc., will not be changed, and the information of the ordinary user B will be recorded, so that the problem of loss and confusion of system or website operation records is perfectly solved.
The above description is only of the preferred embodiments of the present invention, and it should be noted that: it will be apparent to those skilled in the art that various modifications and adaptations can be made without departing from the principles of the invention and these are intended to be within the scope of the invention.